Permission model represents a single permission in the system. It extends Laravel’s Eloquent Model and implements the Spatie\Permission\Contracts\Permission interface.
Properties
The primary key of the permission
The name of the permission
The guard name associated with this permission
Timestamp when the permission was created
Timestamp when the permission was last updated
Relationships
Collection of roles that have this permission
Collection of users who have this permission directly assigned
Static Methods
create
Create a new permission.An array of attributes for the permission. Must include
name. If guard_name is not provided, the default guard will be used.The newly created permission instance
Throws
PermissionAlreadyExists if a permission with the same name and guard already exists.findByName
Find a permission by its name and optionally guard name.The name of the permission to find
The guard name. If not provided, the default guard will be used.
The permission instance
Throws
PermissionDoesNotExist if the permission is not found.findById
Find a permission by its ID and optionally guard name.The ID of the permission to find
The guard name. If not provided, the default guard will be used.
The permission instance
Throws
PermissionDoesNotExist if the permission is not found.findOrCreate
Find an existing permission or create a new one.The name of the permission
The guard name. If not provided, the default guard will be used.
The found or newly created permission instance
Instance Methods
roles
Get all roles that have this permission.A BelongsToMany relationship to the Role model
users
Get all users who have this permission directly assigned (not via roles).A BelongsToMany relationship to the User model
Methods from HasRoles Trait
assignRole
Assign one or more roles to this permission.One or more roles to assign (can be role name, ID, model instance, or array/collection of these)
The permission instance for method chaining
removeRole
Remove one or more roles from this permission.One or more roles to remove
The permission instance for method chaining
syncRoles
Remove all current roles and assign the given ones.One or more roles to sync
The permission instance for method chaining
hasRole
Check if the permission has any of the given roles.One or more roles to check
The guard name to filter by
True if the permission has any of the given roles
hasAllRoles
Check if the permission has all of the given roles.One or more roles to check
The guard name to filter by
True if the permission has all of the given roles
hasAnyRole
Alias tohasRole() but without guard controls.
One or more roles to check
True if the permission has any of the given roles
hasExactRoles
Check if the permission has exactly the given roles and no more.One or more roles to check
The guard name to filter by
True if the permission has exactly these roles
getDirectPermissions
Get all permissions directly coupled to the permission model.Collection of permissions
getRoleNames
Get the names of all roles assigned to this permission.Collection of role names
Query Scopes
role
Scope the query to permissions that have certain roles.One or more roles to filter by
The guard name to filter by
withoutRole
Scope the query to permissions that don’t have certain roles.One or more roles to exclude
The guard name to filter by
Configuration
The Permission model uses the following configuration options:permission.table_names.permissions- The database table name (default:permissions)permission.models.role- The Role model class to use for relationshipspermission.table_names.role_has_permissions- The pivot table for role-permission relationshipspermission.table_names.model_has_permissions- The pivot table for direct user-permission relationships
Events
The following events are fired when roles are attached/detached:Spatie\Permission\Events\RoleAttachedEvent- Fired when a role is assignedSpatie\Permission\Events\RoleDetachedEvent- Fired when a role is removed