roles icon indicating copy to clipboard operation
roles copied to clipboard

Role hasPermission?

Open Davy1992 opened this issue 9 years ago • 2 comments

How to go about checking if a given role has specified permission? For example: $role = Role:find(1); if ($role->hasPermission(3)) { echo "role with id 1 has permission with id 3"; }

Thanks in advance.

Davy1992 avatar Aug 11 '15 07:08 Davy1992

You can do this using default eloquent functions. Retrieve all permissions by doing:

$role = Role::find(1);
$permissions = $role->permissions;

You can use the find method to see if it exists:

if($permissions->find(26) !== null) {
    //has the permission
}

niknetniko avatar Sep 15 '15 17:09 niknetniko

Thank you, that works.

Davy1992 avatar Sep 26 '15 07:09 Davy1992