When attempting to eager load roles with their assigned users from Spatie's laravel-permissions library like this
use Spatie\Permission\Models\Role;
Role::with('users')->get();
This error occurs
Error: Class name must be a valid object or a string in file vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php on line 791
The code above works just fine in the Laravel's PsySH powered Repl Tinker, see this StackOverflow post
So I thought if this only happens in HTTP requests, it must be due to a middleware issue
Am using Laravel Sanctum for API authentication and thus the route is under the middleware auth:sanctum
in routes/api.php
Route::middleware('auth:sanctum')->group(function () {
Route::get('/roles', [RolesController::class, 'index']);
});
It also works if I move the route out of the middleware but I shouldn't, since only authenticated users should be able to access that endpoint
Am guessing this has something to do with the fact that roles get created with the guard_name
as web
in the database by default, but am not sure how to fix it