Property exists but property_exists() return false;
I see you're using Laravel, so I guess this are Eloquent models. They're probably using magic methods to create dynamic properties and methods from your database columns. Take a look here: http://php.net/manual/en/language.oop5.overloading.php
So, instead of having real properties, every time you request a property, they will check if there's any column or relationship and return that instead.
You can get your model attributes with the getAttributes()
method (https://github.com/illuminate/database/blob/master/Eloquent/Concerns/HasAttributes.php#L851)
class Pais
{
public function __get($name) {
if ($name == 'id') {
return 1;
}
}
}
$pais = new Pais();
var_dump($pais->id); // int(1)
var_dump(property_exists($pais, 'id')); // bool(false)
You can convert the model to an array and then use array_key_exists
. Eloquent object properties are set via magic methods, so property_exists
will not work, especially if the property actually does exist but is set to null
.
Example:
$pais = Pais::find(1);
if (array_key_exists('country', $pais->toArray())) {
// do stuff
}
Note the use of toArray
on the model.