This package allows for users to be associated with permissions and roles. Every role is associated with multiple permissions.
A Role and a Permission are regular Eloquent models. They require a name and can be created like this:
If you're using multiple guards the guard_name attribute needs to be set as well. Read about it in the using multiple guards section of the readme.
The HasRoles trait adds Eloquent relationships to your models, which can be accessed directly or used as a base query:
// get a list of all permissions directly assigned to the user
$permissionNames = $user->getPermissionNames(); // collection of name strings
$permissions = $user->permissions; // collection of permission objects// get all permissions for the user, either directly, or from roles, or from both
$permissions = $user->getDirectPermissions();
$permissions = $user->getPermissionsViaRoles();
$permissions = $user->getAllPermissions();
// get the names of the user's roles
$roles = $user->getRoleNames(); // Returns a collection
The HasRoles trait also adds a role scope to your models to scope the query to certain roles or permissions:
$users = User::role('writer')->get(); // Returns only users with the role 'writer'
The role scope can accept a string, a \Spatie\Permission\Models\Role object or an \Illuminate\Support\Collection object.
The same trait also adds a scope to only get users that have a certain permission.
$users = User::permission('edit articles')->get(); // Returns only users with the permission 'edit articles' (inherited or directly)
The scope can accept a string, a \Spatie\Permission\Models\Permission object or an \Illuminate\Support\Collection object.