The bundled Role
model has easy to use methods to manage and assign permissions.
can($permission)
canAtLeast([$permission])
getPermissions()
grantPermission($ids, array $attributes = [], $touch = true)
grantPermissionBySlug($slug)
grantPermissionByResource($resource)
revokePermission($id = null, $touch = true)
revokePermissionBySlug($slug)
revokePermissionByResource($resource)
revokeAllPermissions()
syncPermissions($ids, $detaching = true)
Checks if the role has the given permission.
$role = Role::find(1);
return $role->can('users.create');
Checks if the role has the given permission(s).
At least one permission must be accounted for in order for this to return true
.
$role = Role::find(1);
return $role->canAtleast(['users.create', 'users.view']);
Retrieves an array of assigned permission slugs for the role.
$role = Role::find(1);
return $role->getPermissions();
Grant the given permission to the role.
$role = Role::find(1);
$role->grantPermission(1);
$permissions = Permission::all();
$role->grantPermission($permissions);
Grant the given permission slug to the role.
$role = Role::find(1);
$role->grantPermissionBySlug('create-post');
$permissions = ['create-post', 'view-post'];
$role->grantPermissionBySlug($permissions);
Grant the given permission resource to the role.
$role = Role::find(1);
$role->grantPermissionByResource('Posts');
$resources = ['Users', 'Posts'];
$role->grantPermissionByResource($resources);
Revokes the given permission from the role.
$role = Role::find(1);
$role->revokePermission(1);
$role->revokePermission([1, 2]);
Revokes the given permission slug from the role.
$role = Role::find(1);
$role->revokePermissionBySlug('create-post');
Revokes the given permission resource from the role.
$role = Role::find(1);
$role->revokePermissionByResource('Posts');
##revokeAllPermissions() Revokes all permissions from the role.
$role = Role::find(1);
$role->revokeAllPermissions();
Syncs the given permissions with the role. This will revoke any permissions not supplied.
$role = Role::find(1);
$role->syncPermissions(1);
$role->syncPermissions([1, 2, 3]);