I'm trying to create a policy like the following:
public function view(User $user, PersonalStatement $personalStatement)
{
return $user->applicant->id == $personalStatement->applicant_id || $user->mentor->exists();
}
I then test it with a User model where mentor relationship exists, but applicant relationship does not, and I get the error:
Trying to get property 'id' of non-object
I believe this is because the user->applicant returns null in this case.
What's the best way to evaluate if a model has a relationship where the response is either true or false?
Using exists() method returns null if the relationship does not exist.