Nope, it is not possible working with Laravel Eloquent that way. That error is spot on.
With Dynamic Relationships and Eloquent Collections we can do this:
public function create(User $user)
{
$permissions = $user
->roles
->map(fn (Role $role) => $role->permissions)
->collapse()
->unique();
return $permissions->contains('name', 'create-post');
}
Or we can do everything in one go, with a bit of higher order messages
public function create(User $user)
{
return $user
->roles
->map
->permissions
->collapse()
->unique()
->contains('name', 'create-post');
}
Let me know if those work.
Apparently you are using your own system of authorization. You might have heard about Bouncer and Spatie permissions. Those could make features like this more streamlined.