Role Permissions
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)
can($permission)
Checks if the role has the given permission.
$role = Role::find(1); return $role->can('users.create');
canAtLeast([$permission])
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']);
getPermissions()
Retrieves an array of assigned permission slugs for the role.
$role = Role::find(1); return $role->getPermissions();
grantPermission($ids, array $attributes = [], $touch = true)
Grant the given permission to the role.
$role = Role::find(1);$role->grantPermission(1); $permissions = Permission::all();$role->grantPermission($permissions);
grantPermissionBySlug($slug)
Grant the given permission slug to the role.
$role = Role::find(1);$role->grantPermissionBySlug('create-post'); $permissions = ['create-post', 'view-post'];$role->grantPermissionBySlug($permissions);
grantPermissionByResource($resource)
Grant the given permission resource to the role.
$role = Role::find(1);$role->grantPermissionByResource('Posts'); $resources = ['Users', 'Posts'];$role->grantPermissionByResource($resources);
revokePermission($id = null, $touch = true)
Revokes the given permission from the role.
$role = Role::find(1); $role->revokePermission(1);$role->revokePermission([1, 2]);
revokePermissionBySlug($slug)
Revokes the given permission slug from the role.
$role = Role::find(1); $role->revokePermissionBySlug('create-post');
revokePermissionByResource($resource)
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();
syncPermissions($ids, $detaching = true)
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]);