spatie / laravel-permission

Associate users with roles and permissions
https://spatie.be/docs/laravel-permission
MIT License
12.18k stars 1.78k forks source link

PermissionDoesNotExist error thrown when trying to seed a role with permissions #314

Closed nekromoff closed 7 years ago

nekromoff commented 7 years ago

e.g. RoleSeeder has some basic stuff in it:

$admin = Role::findByName('admin');
        $admin->givePermissionTo('invoices.reinvoicing');

php artisan db:seed --class=RoleSeeder

[Spatie\Permission\Exceptions\PermissionDoesNotExist]
There is no permission named invoices.reinvoicing for guard web.

Only web guard is used.

permissions table: 1 invoices.reinvoicing web 2017-06-09 01:27:13 2017-06-09 01:27:13

nekromoff commented 7 years ago

I just want to add that clearing cache via artisan does not help at all. Even clearing cache just for this module does not help.

francisco-gamonal commented 7 years ago

I have same problem, but searching the method getPermissions in the class Spatie\Permission\PermissionRegistrar:


public function getPermissions(): Collection
{
    return app(Permission::class)->with('roles')->get(); // <- don't use the cache

    return $this->cache->remember($this->cacheKey, config('permission.cache_expiration_time'), function () {
        return app(Permission::class)->with('roles')->get();
    });
}

It works now, but it is a terrible idea to do this.

Update: The solution

php artisan cache:forget spatie.permission.cache
aliworkshop commented 7 years ago

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

cbloss commented 7 years ago

We are having the same problem here. Clearing the cache didn't help. It only fails on one that was newly created called admin.oauth. We tried changing the name, but get the same error. This is done via a migration.

cbloss commented 7 years ago

Note: We discovered that if you pass in a collection and not an array of names, it works. So weird.

code-by-gijs commented 7 years ago

I have the same issue. Seems to be a caching issue. Everything works fine when I set my cache expire time to 0 in the permission config file.

'cache_expiration_time' => 0,

montyclt commented 7 years ago

Is possible disable cache? Can be useful in devs environments.

code-by-gijs commented 7 years ago

@montyclt yes, publish the config file and set "cache_expiration_time" to 0

freekmurze commented 7 years ago

The readme of the package now contains a section on database seeding.

murtazasultani commented 6 years ago

how to solve the error There is no permission named Administer roles & permissions for guard web. Spatie \ Permission \ Exceptions \ PermissionDoesNotExist

meyer59 commented 5 years ago

I had the same issue and all the previous responses didn't worked. The solution was to change the cache driver from file to database and it worked. Be sure to create the cache table php artisan cache:table && php artisan migrate

robvankeilegom commented 5 years ago

Same for me. php artisan cache:clear didn't work. Removing the cache folder in storage/framework did.

mwangiismuthui commented 4 years ago

This worked for me $role = Role::find($id); $role->name = $request->input('name'); $role->save();

$role->permissions()->sync($request->input('permission'));