I'm trying to create permissions and roles using seed and I'm always getting this error:
[Spatie\Permission\Exceptions\PermissionAlreadyExists] A access-users permission already exists for guard web.
The permissions table is empty.
I already clear cache (php artisan cache:clear, php artisan cache:forget spatie.permission.cache, php artisan cache:forget spatie.role.cache), but the error remains.
Can anyone help me with this?
Thank you!
Hi,
Are you sure your seeder doesn't accidentally run twice?
You could add the following code at the beginning of your seeder to make sure the table & cache start empty before being seeded:
Schema::disableForeignKeyConstraints();
DB::table('permissions')->truncate();
Schema::enableForeignKeyConstraints();
app(PermissionRegistrar::class)->forgetCachedPermissions();
@AlexVanderbist
Thank you for your reply, but that solution didn't work. I still got the same message; "[Spatie\Permission\Exceptions\PermissionAlreadyExists] A access-users permission already exists for guard web."
This is the code I'm using on seed file:
Schema::disableForeignKeyConstraints();
DB::table('permissions')->truncate();
Schema::enableForeignKeyConstraints();
app(PermissionRegistrar::class)->forgetCachedPermissions();
// Reset cached roles and permissions
app()['cache']->forget('spatie.permission.cache');
////////// Create permissions //////////
$permissions = [];
// Users
$permissionGroup = PermissionGroup::create(['readable_name' => 'Users']);
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'access-users', 'readable_name' => 'Access users']);
$permissions[] = 'access-users';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'create-users', 'readable_name' => 'Create users']);
$permissions[] = 'create-users';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'edit-users', 'readable_name' => 'Edit users']);
$permissions[] = 'edit-users';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'delete-users', 'readable_name' => 'Delete users']);
$permissions[] = 'delete-users';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'restore-users', 'readable_name' => 'Restore users']);
$permissions[] = 'restore-users';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'access-profile', 'readable_name' => 'Access profile']);
$permissions[] = 'access-profile';
Permission::create(['group_id' => $permissionGroup->id, 'name' => 'edit-profile', 'readable_name' => 'Edit profile']);
$permissions[] = 'edit-profile';
////////// Create roles and assign existing permissions //////////
$role = Role::create(['name' => 'administrator', 'readable_name' => 'Administrator']);
$role->syncPermissions($permissions);
Any news on this subject?
Since there is no group_id or readable_name field in this package's Permission model, it's clear that you've extended the class and added additional functionality.
I suspect the problem is in your customized Permission model. Can you post the code for that? And also post any other code you've overridden/extended?
@drbyte No changes made to the model. I just added two more fields to the migration.
Anyway, I solved the problem.
While logged to the server as a normal user, this problem as started after a few migrations resets and seeding.
I try to log as root, run migration and seed and it worked!
After that, I logged again as a normal user, run migrations again and everything went ok.
So, problem solved! :)
Thank's for the supports, guys.
i've got the same issue. You don't need to do the migration as root, just the cache:forget as root, then the migration (and/or seed) with your regular user.
@ThibaultModat Thank you ! On my macbook air ,it's works.you are great !!!
Deleting cache as root worked for me on Ubuntu 18.04
Most helpful comment
i've got the same issue. You don't need to do the migration as root, just the cache:forget as root, then the migration (and/or seed) with your regular user.