Laravel Spatie How to Eager load Role And Permissions at once based on user

1.3k Views Asked by At

its possible to eager load role and permissions at once using with()? im doing like code bellow.but not working ,if any of you guys can suggest me an articles or ways to solve this will be helpfull

    $user = User::with('roles','permissions')->when(request()->q, function($user) {
        $user = $user->where('name', 'like', '%'. request()->q . '%');
    })->latest()->paginate(20);
1

There are 1 best solutions below

0
On

when we are using team=1, we have to use this function before any running query.

Route::get('/user-infos',function(){
   setPermissionsTeamId(1);
   $user = User::with('roles')->get();
   return $user;
});