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

david19's avatar

Laravel Policy with Roles and Permissions

Hello Team. I created my own Roles and Permissions. Very standart ManyToMany Pivot table.

Tables in database:

roles
role_user
permissions
permission_role

In blade i can fetch my roles like this:

@foreach($users as $user)
	@foreach($user->roles as $role)
	@endforeach
@endforeach

Same for permissions:

@foreach($roles as $role)
	@foreach($role->permissions as $permission)
	@endforeach
@endforeach

It works very fine :)

Now i created a Model Policy. And here i will check: Have the User a Role with this permission, but i make a mistake.

public function viewAny(User $user)
    {
       
    	$permission = Permission::where('name','user_index')->first();
        $roles = $user->roles()->with('permissions')->get();
        
	dd($permission); works!
        dd($roles); works!
       How i can check, if the user has a role with this permission name "user_index"?
  
        
    }

Many thanks :)

0 likes
2 replies

Please or to participate in this conversation.