Skip to content

Role Permissions

Carlos G. Camacho O edited this page May 15, 2018 · 1 revision

The bundled Role model has easy to use methods to manage and assign permissions.

can($permission)

Checks if the role has the given permission(s). You may pass either a string or an array of permissions to check for. In the case of an array, ALL permissions must be accounted for in order for this to return true.

$role = Role::find(1);

return $role->can('access.admin');

getPermissions()

Retrieves an array of assigned permission slugs for the role.

$role = Role::find(1);

return $role->getPermissions();

assignPermission($permissionId)

Assigns the given permission to the role.

$role = Role::find(1);

$role->assignPermission(1);

$role->save();

revokePermission($permissionId)

Revokes the given permission from the role.

$role = Role::find(1);

$role->revokePermission(1);

$role->save();

revokeAllPermissions()

Revokes all permissions from the role.

$role = Role::find(1);

$role->revokeAllPermissions();

$role->save();

syncPermissions([$permissionIds])

Syncs the given permissions with the role. This will revoke any permissions not supplied.

$role = Role::find(1);

$role->syncPermissions([1, 2, 3]);

$role->save();