Role model represents a role in the system. It extends Laravel’s Eloquent Model and implements the Spatie\Permission\Contracts\Role interface.
Properties
The primary key of the role
The name of the role
The guard name associated with this role
Timestamp when the role was created
Timestamp when the role was last updated
Relationships
Collection of permissions assigned to this role
Collection of users who have this role
Static Methods
create
Create a new role.An array of attributes for the role. Must include
name. If guard_name is not provided, the default guard will be used.The newly created role instance
Throws
RoleAlreadyExists if a role with the same name and guard already exists.findByName
Find a role by its name and optionally guard name.The name of the role to find
The guard name. If not provided, the default guard will be used.
The role instance
Throws
RoleDoesNotExist if the role is not found.findById
Find a role by its ID and optionally guard name.The ID of the role to find
The guard name. If not provided, the default guard will be used.
The role instance
Throws
RoleDoesNotExist if the role is not found.findOrCreate
Find an existing role or create a new one.The name of the role
The guard name. If not provided, the default guard will be used.
The found or newly created role instance
Instance Methods
permissions
Get all permissions assigned to this role.A BelongsToMany relationship to the Permission model
users
Get all users who have this role.A BelongsToMany relationship to the User model
hasPermissionTo
Determine if the role has the given permission.The permission to check (can be permission name, ID, model instance, or enum)
The guard name. If not provided, the role’s guard will be used.
True if the role has the permission
Throws
PermissionDoesNotExist if the permission doesn’t exist or GuardDoesNotMatch if guards don’t match.Methods from HasPermissions Trait
givePermissionTo
Grant one or more permissions to this role.One or more permissions to grant (can be permission name, ID, model instance, or array/collection of these)
The role instance for method chaining
syncPermissions
Remove all current permissions and assign the given ones.One or more permissions to sync
The role instance for method chaining
revokePermissionTo
Revoke one or more permissions from this role.One or more permissions to revoke
The role instance for method chaining
checkPermissionTo
Check if the role has the given permission without throwing exceptions.The permission to check
The guard name
True if the role has the permission, false otherwise
hasAnyPermission
Check if the role has any of the given permissions.One or more permissions to check
True if the role has at least one of the permissions
hasAllPermissions
Check if the role has all of the given permissions.One or more permissions to check
True if the role has all of the permissions
hasDirectPermission
Check if the role has the given permission directly assigned.The permission to check
True if the role has the permission directly
hasAllDirectPermissions
Check if the role has all of the given permissions directly assigned.One or more permissions to check
True if the role has all permissions directly
hasAnyDirectPermission
Check if the role has any of the given permissions directly assigned.One or more permissions to check
True if the role has any permission directly
getAllPermissions
Get all permissions assigned to this role.Collection of all permissions
getPermissionNames
Get the names of all permissions assigned to this role.Collection of permission names
forgetCachedPermissions
Clear the permission cache.Query Scopes
permission
Scope the query to roles that have certain permissions.One or more permissions to filter by
withoutPermission
Scope the query to roles that don’t have certain permissions.One or more permissions to exclude
Team Support
When team support is enabled in the configuration, roles are automatically scoped to the current team. The package handles team filtering automatically in all queries and relationships.Configuration
The Role model uses the following configuration options:permission.table_names.roles- The database table name (default:roles)permission.models.permission- The Permission model class to use for relationshipspermission.table_names.role_has_permissions- The pivot table for role-permission relationshipspermission.table_names.model_has_roles- The pivot table for user-role relationshipspermission.teams- Enable team supportpermission.teams_key- The foreign key column for team relationships
Events
The following events are fired when permissions are attached/detached:Spatie\Permission\Events\PermissionAttachedEvent- Fired when a permission is grantedSpatie\Permission\Events\PermissionDetachedEvent- Fired when a permission is revoked
Wildcard Permissions
When wildcard permissions are enabled in the configuration, thehasPermissionTo method supports wildcard pattern matching:
Wildcard permissions must be enabled in the configuration file.