Laravel request User with related table

241 Views Asked by At

I am quite new to this but I hope I can get some help or explanation how to solve my problem.

How could I request a User with a related table already included in the result? For example, I have the User model and Roles model. How can I automatically include Roles when requesting User. I am using Spatie Roles and Permissions.

I can do User::with('roles')->get(); but how could I include roles with this request Request $request->user();

return array_merge(parent::share($request), [
        'auth' => [
            'user' => $request->user(),
            'role' => $request->user()->getRoleNames(),
        ],
    ]);

If I request them separately then role gets added to auth and also user has role relation available.

I have HasRoles included in my User model.

I would like to have Roles "attached" to the User model when I request it to be available everywhere and I wouldn't have to request Roles separately. I am using inertiajs, so I want the roles to be included with the authenticated user.

0

There are 0 best solutions below