Home > Software engineering >  The given role or permission should use guard Spatie permissions?
The given role or permission should use guard Spatie permissions?

Time:01-03

I use this code to create a role and assign it:

$role = \Spatie\Permission\Models\Role::create(['name' => 'doctor']);
$doctor = \App\Models\Doctor::find(1);
$doctor->assignRole($role);

But I got the error:

"message": "The given role or permission should use guard `` instead of sanctum."

Inside auth.php, there is:

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

How to fix that?

CodePudding user response:

The error message is saying that the role or permission you are trying to assign is using the sanctum guard, but it should be using the web guard.

To fix this, you can specify the guard when you create the role:

$role = \Spatie\Permission\Models\Role::create(
    ['name' => 'doctor', 'guard_name' => 'web']
);

I hope this helps.

  • Related