Laravel Sanctum and Laravel Permissions #2661
Replies: 3 comments 2 replies
-
It's difficult to know whether the problem is with your Controller, or with your custom model, or any other place. Please post all the code necessary to recreate the problem on a new Laravel app. |
Beta Was this translation helpful? Give feedback.
-
I got the same issue too. and I add // config/auth.php
return [
/** some other config ignored **/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'session',
'provider' => 'users',
],
'sanctum' => [
'driver' => 'sanctum',
'provider' => 'users', // <== null for laravel-permission get the relations
],
],
/** some other config ignored **/
]; now, I can use return Role::withCount('users')->get(); without errors. hope this helps you. |
Beta Was this translation helpful? Give feedback.
-
I am using Laravel Permissions to create a Roles system in my web application that uses Laravel for the backend and Quasar with Vue on the front end. I have an api route that points to the index function of RoleController and it returns all the roles with their respective permissions and users related to each role.
Everything works correctly until I assign the auth:sanctum middleware to the api route. It is at that moment that the errors begin, specifically with the users relationship of the Laravel Permissions package. I already verified that it is only this relationship that does not work when I am using the auth:sanctum middleware in the api route of the index function.
The error it returns is the following:
{ "message": "Class name must be a valid object or a string", "exception": "Error", "file": "D:\\1-TRABAJO\\PROYECTOS\\ZUNspad2.0\\backend\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasRelationships.php", "line": 793, }
Please, what could I be overlooking, I need to fix this error. The relationship exists and works but only when I do not use the auth:sanctum middlewareBeta Was this translation helpful? Give feedback.
All reactions