$users = User :: permission ( 'edit articles' ) -> get () // Returns only users with the permission 'edit articles' (inherited or directly) $usersWhoCannotEditArticles = User :: withoutPermission ( 'edit articles' ) -> get () // Returns all users without the permission 'edit articles' (inherited or directly) The same trait also adds scopes to only get users that have or don't have a certain permission. The role and withoutRole scopes can accept a string, a \Spatie\Permission\Models\Role object or an \Illuminate\Support\Collection object. The HasRoles trait also adds role and withoutRole scopes to your models to scope the query to certain roles or permissions: $users = User :: role ( 'writer' ) -> get () // Returns only users with the role 'writer' $nonEditors = User :: withoutRole ( 'editor' ) -> get () // Returns only users without the role 'editor' 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 #Scopes Read about it in the using multiple guards section of the readme. If you're using multiple guards then the guard_name attribute must be set as well. Multiple permissions can be synced to a role using either of these methods: $role -> syncPermissions ($permissions) $permission -> syncRoles ($roles) #Remove Permission From A RoleĪ permission can be removed from a role using either of these methods: $role -> revokePermissionTo ($permission) $permission -> removeRole ($role) #Guard Name They require a name and can be created like this: use Spatie\Permission\Models\Role use Spatie\Permission\Models\Permission $role = Role :: create () $permission = Permission :: create () #Assign A Permission To A RoleĪ permission can be assigned to a role using either of these methods: $role -> givePermissionTo ($permission) $permission -> assignRole ($role) #Sync Permissions To A Role ![]() ![]() Every role is associated with multiple permissions.Ī Role and a Permission are regular Eloquent models. This package allows for users to be associated with permissions and roles. First, add the Spatie\Permission\Traits\HasRoles trait to your User model(s): use Illuminate\Foundation\Auth\User as Authenticatable use Spatie\Permission\Traits\HasRoles class User extends Authenticatable #Create A Permission
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |