Laravel-permission: PermissionDoesNotExist error thrown when trying to seed a role with permissions

Created on 9 Jun 2017  路  13Comments  路  Source: spatie/laravel-permission

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

Most helpful comment

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

All 13 comments

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.

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

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.

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

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,

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

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

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

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

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wreighsantos picture wreighsantos  路  4Comments

ghost picture ghost  路  3Comments

Dreambox13 picture Dreambox13  路  3Comments

holymp2006 picture holymp2006  路  4Comments

MichalKrakow picture MichalKrakow  路  4Comments