Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kartsa's avatar

Checking if Laravel model property is set?

Is there any way to check if a certain field in a Laravel model object is set?

Consider the following situation created in tinker:

>>> $user = new App\User;
=> App\User {#3035}

>>> $user->email = '[email protected]';
=> "[email protected]"

>>> $user->name = null;
=> null

>>> $user;
=> App\User {#3035
     email: "[email protected]",
     name: null,
   }

>>> $user->email;
=> "[email protected]"

>>> $user->name;
=> null

>>> $user->password;
=> null

$user->name is set, but $user->password is not, but both return null. How can I distinguish these from each other? isset() or property_exists() do not.

0 likes
2 replies
Tomi's avatar

For me they are both set to null, not sure what your question is?

Do you mean if they where set to any value at some time?

Kartsa's avatar
Kartsa
OP
Best Answer
Level 1

Yes, set to any value, even null. One was set to null, the other one not.

Anyways, I found one way to check it. I have to convert the model to an array and then check the existence of a key.

>>> array_key_exists('name', $user->toArray())
=> true
>>> array_key_exists('password', $user->toArray())
=> false

Maybe not the cleanest and most efficient way, but unless there's a better way, I'm using this.

1 like

Please or to participate in this conversation.