laravel-permission: PermissionAlreadyExists - A `users_manage` permission already exists for guard `web`.

I reset my database and deleted all the tables. When I rerun the migration with the seeders I get an error that a permission already exists. php artisan migrate:reset runs fine but does not fix the error. How can I reseed the database and start fresh? Thank you

php artisan migrate --seed
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2017_07_12_145959_create_permission_tables
Migrated:  2017_07_12_145959_create_permission_tables
Migrating: 2017_07_12_230054_create_products_table
Migrated:  2017_07_12_230054_create_products_table
Seeding: PermissionSeed

                                                               
  [Spatie\Permission\Exceptions\PermissionAlreadyExists]       
  A `users_manage` permission already exists for guard `web`.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16

Most upvoted comments

I had to clear the permission cache!

Running:

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

Got me going again! Thank you

https://github.com/spatie/laravel-permission/issues/231

Help for me:

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

sudo chmod 777 -R /var/www/html/project/storage/

Try php artisan migrate:refresh --seed. Also ensure in your PermissionSeed that you are ONLY defining one instance of users_manage. Let me know how it goes!

OR you can paste your PermissionSeed here so we can see if the problem is in there.

😃

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

Same issue on Laravel 7.1X when I tried to refresh the migrations, and this worked for me:

sudo chmod 777 -R /var/www/project/storage/
php artisan cache:forget spatie.permission.cache
php artisan cache:clear
php artisan db:seed --class=YourSeederClass

Better to use php artisan permission:cache-reset as documented https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/

Besides the clearing of the cache and all that, try to check if permissions and roles already exist. This way you can run your seeder as often as you like without errors … even extend it and rerun it, if you forgot something 😉

try {
	$a_role = Role::findByName('my_role');
} catch (Exception $e) {
	$a_role = Role::create(['name' => 'my_role']);
}

and

try {
	$a_permission = Permission::create(['name' => 'my_permission']);
} catch (Exception $e) {
	$a_permission = 'my_permission';
}

and

if (!$a_role->hasPermissionTo($a_permission)) {
	$a_role->givePermissionTo($a_permission);
}