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

Desssha's avatar

Call to a member function permissions() on null ,App\Models\User::hasPermission

i dont know why this error in another browser work ok and not show this error and say problem is

User Model

public function role() { return $this->belongsTo(Role::class); }

public function hasPermission($name)
{
    return $this->role->permissions()->where('name',$name)->exists();
}

Role Model public function permissions() { return $this->belongsToMany(Permission::class); }

i make roles and permission and pivot table an use Policy and this my policy

public function viewAll(User $user) { return $user->hasPermission('view_category'); }

and this controller

public function AllCat() { $this->authorize("viewAll",Category::class); return $this->categoryInterface->AllCat(); }

in google chrome work ok and brave but edge show this error .

0 likes
4 replies
tykus's avatar

So, in here $this->role is null

public function hasPermission($name)
{
    return $this->role->permissions()->where('name',$name)->exists();
}

This should not be affected by the browser; are you working with the same User record? What is $this->role in the context of a User instance?

Desssha's avatar

@tykus i try with user not have permission give me this error and this all method in model user This should not be affected by the browser; are you working with the same User record? What is $this->role in the context of a User instance?

tykus's avatar
tykus
Best Answer
Level 104

@Desssha I asked about $this->role - it seems that a given User does not have a Role, hence null. If a User without a Role does not have any permissions, change the hasPermission method to return early:

public function hasPermission($name)
{
    if (! $this->role) {
        return false;
    }
    return $this->role->permissions()->where('name',$name)->exists() :;
}
Desssha's avatar

@tykus wooow man thanks a lot you are Great !! it work . over than 2 week in this permission to understand how it work without packge . thxs man.

Please or to participate in this conversation.