1. 安装扩展包
composer require "spatie/laravel-permission:~2.7"
生成数据库迁移文件:
php artisan vendor:publish –provider=”Spatie/Permission/PermissionServiceProvider” –tag=”migrations”
在 migration 目录下可看到相关表信息,执行数据库迁移
php artisan migrate
生成配置信息:
php artisan vendor:publish --provider="Spatie/Permission/PermissionServiceProvider" --tag="config"
在 User 模型下加载
..... use Spatie/Permission/Traits/HasRoles; // use class User extends Authenticatable { use HasRoles; // 加载角色相关信息 .....
创建角色 和用户
use Spatie/Permission/Models/Role; use Spatie/Permission/Models/Permission; ..... $role = Role::create(['name' => 'writer']); // 创建角色 $permission = Permission::create(['name' => 'edit articles']);// 创建权限
使用
为用户添加权限
$user->givePermissionTo('edit articles');
为用户添加角色
$user->assignRole('writer'); $user->assignRole(['writer', 'admin']);
给用户删除权限
$user->revokePermissionTo('edit articles');
给角色添加权限
$role->givePermissionTo('edit articles');
为角色添加权限
$role->givePermissionTo('edit articles');
撤销一个权限 并且 添加一个新权限
$user->syncPermissions(['edit articles', 'delete articles']);
获得当前用户的角色集合
$user->getRoleNames();
将多个角色同步到权限
$role->syncPermissions($permissions); // @param array $permissions $permission->syncRoles($roles);
从角色中删除权限
$role->revokePermissionTo($permission); $permission->removeRole($role);
获取当前的用户的权限列表
$permissions = $user->permissions;
获取用户的所有权限,或者直接权限 (odel_has_permissions),或者从角色获取,或者从两者获取
$permissions = $user->getDirectPermissions(); $permissions = $user->getPermissionsViaRoles(); $permissions = $user->getAllPermissions();
获取用户的角色集合 collection
$roles = $user->getRoleNames(); // Returns a collection
返回指定角色的用户 | Returns only users with the role ‘writer’
$users = User::role('writer')->get(); //
返回指定权限的用户
$users = User::permission('edit articles')->get();
用户有什么角色
$user->hasRole('writer');
验证类
检查是否有某个权限
$user->hasPermissionTo('edit articles'); $user->can('edit articles');
检查是否有某个角色 | 或者列
$user->hasRole('writer'); $user->hasAnyRole(Role::all()); $user->hasAllRoles(Role::all());
传递 id 值进行判断是否有某个权限
$user->hasPermissionTo('1'); $user->hasPermissionTo(Permission::find(1)->id); $user->hasPermissionTo($somePermission->id);
是否拥有一组权限
$user->hasAnyPermission(['edit articles', 'publish articles', 'unpublish articles']);
检查一个角色是否有某些权限 | 删除某些权限
$role->hasPermissionTo('edit articles'); $role->revokePermissionTo('edit articles'); // 删除
模板使用
@role('writer') I am a writer! @else I am not a writer... @endrole ------------------------ @hasrole('writer') I am a writer! @else I am not a writer... @endhasrole ------------------------ @can('edit articles') // 拥有某个权限 可执行操作 // @endcan
数据填充
use Illuminate/Database/Seeder; use Spatie/Permission/Models/Role; use Spatie/Permission/Models/Permission; class RolesAndPermissionsSeeder extends Seeder { public function run() { // Reset cached roles and permissions app()['cache']->forget('spatie.permission.cache'); // create permissions Permission::create(['name' => 'edit articles']); Permission::create(['name' => 'delete articles']); Permission::create(['name' => 'publish articles']); Permission::create(['name' => 'unpublish articles']); // create roles and assign created permissions $role = Role::create(['name' => 'writer']); $role->givePermissionTo('edit articles'); $role = Role::create(['name' => 'moderator']); $role->givePermissionTo(['publish articles', 'unpublish articles']); $role = Role::create(['name' => 'super-admin']); $role->givePermissionTo(Permission::all()); } }
提示:如果在数据库权限相关信息表的修改,必须掉用清除 缓存的方法
// 命令删除 php artisan cache:forget spatie.permission.cache app()['cache']->forget('spatie.permission.cache');
【相关推荐:laravel视频教程】