Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Danlog's avatar

Passport - Spatie Roles and Permissions "role not found"

I'm trying to use Laravel Passport, therefore I have to handle the set-up differently.

What I've tried so far:

Inside User.php:

protected $guard_name = 'web'; // this works, but not while trying to use API; functions like logging in stop working

if I try

protected $guard_name = 'api'; // doesn't work for me

and if I try

protected $guard_name = 'passport'; // doesn't work for me either

Inside auth.php I got the following:

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
...
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
            'hash' => true,
        ],
    ],
...

How can I fix this issue?

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're facing is related to the configuration of Laravel Passport and Spatie Roles and Permissions. To fix this issue, you need to make sure that the guard name used in your User model matches the guard name defined in the auth.php configuration file.

In your case, you have defined the api guard in the auth.php file, so you should set the $guard_name property in your User model to 'api'. However, the guard_name property is not used by Laravel Passport directly. Instead, it is used by Spatie Roles and Permissions package to determine the guard name for role and permission checks.

To make it work, you need to modify the User model as follows:

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasApiTokens, HasRoles;

    protected $guard_name = 'api';

    // Rest of your code...
}

By adding the HasRoles trait to your User model and setting the $guard_name property to 'api', you are instructing Spatie Roles and Permissions to use the api guard for role and permission checks.

Make sure you have installed the Spatie Roles and Permissions package and have run the necessary migrations before using the HasRoles trait.

Once you have made these changes, you should be able to use Laravel Passport with Spatie Roles and Permissions without encountering the "role not found" error.

Please or to participate in this conversation.