4

I have cloned marketplace repo and did the appropriate configurations. When I use php artisan db:seed i get this error

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');
}

I have cleared user permissions and roles from cache both as a normal user and as root but its not helping. What am I doing wrong and how do I go about it?

apokryfos
  • 38,771
  • 9
  • 70
  • 114
davidkihara
  • 493
  • 1
  • 10
  • 30
  • 2
    Use `php artisan permission:cache-reset` on terminal or `app()->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();` on your seeder, as documented [here](https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/#manual-cache-reset) – porloscerros Ψ May 11 '20 at 12:06
  • It worked on my project – Lutfor Rahman Jan 25 '22 at 10:51

3 Answers3

12

You can use artisan commands to clear spatie permission cache.

php artisan cache:forget spatie.permission.cache 
php artisan cache:clear
suba
  • 1,320
  • 15
  • 22
1

This error is caused as a result of permissions previously cached. Running php artisan cache:clear fixes it.

Mena
  • 1,873
  • 6
  • 37
  • 77
0

You can check , no have a twice rol assign

  $permission = Permission::create(['name' => 'companies.index'])->assignRole($role1);
$permission = Permission::create(['name' => 'companies.index'])->assignRole($role1);

You can check if you cache are clear

php artisan cache:forget spatie.permission.cache 
php artisan cache:clear
Beowulfdgo
  • 141
  • 1
  • 1
  • 9