basic usage | laravel-permission | spatie


本站和网页 https://spatie.be/docs/laravel-permission/v4/basic-usage/basic-usage 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Basic Usage | laravel-permission | Spatie
SPATIE Products Open Source Courses
Web Development
Vacancies
About
Blog
Docs
Guidelines
Log in
Laravel-permission
Basic-usage
Basic Usage
v6 (main)
v5 (v5)
v4 (v4)
v3 (v3)
Edit
Other versions for crawler
v6
v5
v4
v3
Introduction
Support us
Prerequisites
Installation in Laravel
Installation in Lumen
Upgrading
Questions and issues
Changelog
About us
Direct Permissions
Using Permissions via Roles
Wildcard permissions
Blade directives
Defining a Super-Admin
Using multiple guards
Using artisan commands
Using a middleware
Example App
Best Practices
Roles vs Permissions
Model Policies
Performance Tips
Advanced usage
Testing
Database Seeding
Exceptions
Extending
Cache
UUID
PhpStorm Interaction
Other
Timestamps
UI Options
This is the documentation for
but the latest version is
You can switch versions in the menu
on the left
at the top
Check your current version with the following command:
composer show spatie/laravel-permission
First, add the
Spatie\Permission\Traits\HasRoles
trait to your
User
model(s):
use
Illuminate\Foundation\Auth\User
as
Authenticatable
class
extends
HasRoles
// ...
This package allows for users to be associated with permissions and roles. Every role is associated with multiple permissions.
Role
and a
Permission
are regular Eloquent models. They require a
name
and can be created like this:
Spatie\Permission\Models\Role
Spatie\Permission\Models\Permission
$role
::
create
(['
' => '
writer
']);
$permission
edit articles
A permission can be assigned to a role using 1 of these methods:
->
givePermissionTo
);
assignRole
Multiple permissions can be synced to a role using 1 of these methods:
syncPermissions
$permissions
syncRoles
$roles
A permission can be removed from a role using 1 of these methods:
revokePermissionTo
removeRole
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
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
// collection of permission objects
// get all permissions for the user, either directly, or from roles, or from both
getDirectPermissions
getPermissionsViaRoles
getAllPermissions
// get the names of the user's roles
getRoleNames
// Returns a collection
trait also adds a
role
scope to your models to scope the query to certain roles or permissions:
$users
('
')->
get
// Returns only users with the role 'writer'
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.
permission
// Returns only users with the permission 'edit articles' (inherited or directly)
The scope can accept a string, a
\Spatie\Permission\Models\Permission
Eloquent
Since Role and Permission models are extended from Eloquent models, basic Eloquent calls can be used as well:
$all_users_with_all_their_roles
with
roles
$all_users_with_all_direct_permissions
$all_roles_in_database
all
()->
pluck
');
$users_without_any_roles
doesntHave
$all_roles_except_a_and_b
whereNotIn
', ['
role A
', '
role B
'])->
Check out our self-hosted email marketing solution with API:
Mailcoach
Learn more
×
0" style="display: none;">
Kruikstraat 22, Box 12
2018 Antwerp, Belgium
info@spatie.be
+32 3 292 56 79
GitHub
Instagram
LinkedIn
Twitter
Mastodon
YouTube
Privacy
Disclaimer
Click to call us
this.selectedHit = 0)"
@keyup.down.prevent="selectedHit == 0 - 1 ? selectedHit = 0 : selectedHit++; document.getElementById('hit-' + selectedHit).scrollIntoView({behavior: 'smooth', block: 'nearest', inline: 'nearest'}); document.getElementById('hit-' + selectedHit).focus()"
@keyup.up.prevent="selectedHit == 0 ? selectedHit = 0 - 1 : selectedHit--; document.getElementById('hit-' + selectedHit).scrollIntoView({behavior: 'smooth', block: 'nearest', inline: 'nearest'}); document.getElementById('hit-' + selectedHit).focus()"
@keyup.slash.window="$refs.search.focus()"
class="relative max-w-full max-h-[50vh]"
style="width: 48rem; min-height: 16rem"
ESC
Enter a search term to find results in the documentation.