Spatie \ Permission \ Exceptions \ PermissionAlreadyExists:卫队“ web”的“编辑列表”权限已存在

时间:2020-05-11 09:15:59

标签: laravel spatie

我已经克隆了marketplace存储库并进行了适当的配置。当我使用php artisan db:seed时出现此错误

david@david-Lenovo-G50-30:~/projects/marketplacekit$ php artisan db:seed
Seeding: UsersTableSeeder
Seeding: RolesAndPermissionsSeeder

Spatie\Permission\Exceptions\PermissionAlreadyExists  : A `edit listing` permission already exists for guard `web`.

at /home/david/projects/marketplacekit/vendor/spatie/laravel-permission/src/Exceptions/PermissionAlreadyExists.php:11
 7| class PermissionAlreadyExists extends InvalidArgumentException
 8| {
 9|     public static function create(string $permissionName, string $guardName)
10|     {    
11|         return new static("A `{$permissionName}` permission already exists for guard`{$guardName}`.");
12|     }
13| }
14| 

  Exception trace:

1   Spatie\Permission\Exceptions\PermissionAlreadyExists::create("edit listing", "web")
  /home/david/projects/marketplacekit/vendor/spatie/laravel-permission/src/Models/Permission.php:42

2   Spatie\Permission\Models\Permission::create(["edit listing", "web"])
  /home/david/projects/marketplacekit/database/seeds/RolesAndPermissionsSeeder.php:20

Please use the argument -v to see more details.

RolesandPermissionsSeeder.php

    public function run(){

    //Reset cached roles and permissions
    app()['cache']->forget('spatie.permission.cache');

    //create permissions
    Permission::create(['name' => 'edit listing']);
    Permission::create(['name' => 'publish listing']);
    Permission::create(['name' => 'unpublish listing']);
    Permission::create(['name' => 'disable listing']);
    Permission::create(['name' => 'ban user']);

   // create roles and assign created permissions

    $role = Role::create(['name' => 'member']);

    $role = Role::create(['name' => 'editor']);
    $role->givePermissionTo(['edit listing', 'publish listing', 'unpublish listing']);

    $role = Role::create(['name' => 'moderator']);
    $role->givePermissionTo(['edit listing', 'disable listing', 'publish listing', 'unpublish listing', 'ban user']);

    $role = Role::create(['name' => 'admin']);
    $role->givePermissionTo(Permission::all());

    $user = \App\Models\User::first();
    $user->assignRole('admin');
}

我已经以普通用户和root用户的身份从缓存中清除了用户权限和角色,但这无济于事。我在做什么错了,我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以使用artisan命令清除Spatie权限缓存。

php artisan cache:forget spatie.permission.cache 
php artisan cache:clear