Sanctum Guard Error With using Spatie Package in laravel

220 Views Asked by At

I receive a Sanctum guard error when I try to find a specific Role_id or Permission_id using a Spatie package in Laravel. It was working fine normally when I try to get all roles or permissions but it make an error only when I try to search for a specific id using Sanctum.

Controller

public function create()
{
    $role = Role::orderBy('id', 'DESC')->get();
    $role_user = Role::findById(3);


    return response([
        'status' => true,
        'role' => $role,
        'role_user' => $role_user
    ], 200);
}

Note: it works when I changed the guard manually in db from web to sanctum.

I am using Laravel 10. I have Installed Sanctum for Rest Apis, Spatie for ACL. basically it works fine but it give an error when i try to find specific role or permision by id.

i have tried change my guard in user but it not work, i tried to change guard manually in db for checking it was working but it's not proper good thing to do that.

1

There are 1 best solutions below

0
On

Basically, It is a problem with the Sanctum guard. The Sanctum provider configures its provider to null.

Solution

go to config/auth.php and add sanctum guard into the guards array like the example snippet given below:

'guards' => [
        'sanctum' => [
            'driver' => 'sanctum',
            'provider' => 'users'
        ]
    ],

Note: Don't remove web guard from guards array.

For more details related to this error please visit following Github Issue.

I hope that will fix your problem.