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`.
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.
:)
Same error with php artisan migrate:refresh --seed
My PermissionSeed file looks like this:
<?php
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
class PermissionSeed extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Permission::create(['name' => 'users_manage']);
}
}
If I comment out the Permission::create seed it moves onto the RoleSeed and I get an error: There is no permission named 'users_manage' for guard 'web'.
The RoleSeed looks like this:
<?php
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
class RoleSeed extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$role = Role::create(['name' => 'administrator']);
$role->givePermissionTo('users_manage');
}
}
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
Same problem here.
Exact same problem.
Your solution worked, thats great.
Hope the devs find a solution, 'cause its not so cool to clear the cache every :refresh --seed
Anyway, thank you for the fix.
php artisan cache:forget spatie.role.cache is also needed
It didn't worked for me :(
I created the permissions in the DB, not using seed.
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);
}
sudo chmod 777 -R /var/www/html/project/storage/
Help for me:
sudo php artisan cache:forget spatie.permission.cache && sudo php artisan cache:clear
Cache::flush('spatie.permission.cache');
Cache::flush('spatie.role.cache');
Better to use php artisan permission:cache-reset as documented
https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/
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
Create the below function
function isRoleExist($role_name){
return Count(Role::findByName($role_name)->get()) > 0;
}
Then use it like
```
if(!isRoleExist('manager'){
$role = Role::create(['name' => 'Admin']);
}
````
Most helpful comment
I had to clear the permission cache!
Running:
Got me going again! Thank you
https://github.com/spatie/laravel-permission/issues/231