Reputation: 742
Here's my code:
if ($user->role == NULL){
$user->setRole(Roles::Company);
$user->save();//I can see that it works,the database is updated
$user->fresh(['role']);
}
if ($user->role->id == Roles::Company)//error
//do something
On the line with error I can see this:
Trying to get property of non-object
I see that roles are null, but I don't know why and how to change this behavior. After refreshing page everything works just fine
Upvotes: 3
Views: 79
Reputation: 9988
What you need to do is to get fresh user from fresh
methodf like this:
$user = $user->fresh(['role']);
This method refresh a model and returns new instance (it isn't refreshed in the current instance).
Upvotes: 1