Search code examples
laravellaravel-permission

There is no role named `admin`. laravel


i use this package :

https://github.com/spatie/laravel-permission/tree/v2

code :

     $user=User::find(2);
    $user->assignRole('admin');

and when i assign admin role to user I'm dealing with this error

There is no role named admin.Spatie\Permission\Exceptions\RoleDoesNotExist

this is my default guard in auth.php :

    <?php

return [

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

this is my roles table :

enter image description here

this is my role_has_permission table

enter image description here

and this is my permission table :

enter image description here


Solution

  • just add this protected property to your user model(or whatever model you are using for assigning permissions and roles).

    protected $guard_name = 'api';