0

Let's suppose I have 20 users whose roles are admin, agent, manager and customer. While fetching all 20 users, it fired 21 queries. I want to eager load this.

An example of a query out of 20 fired queries while fetching all users.

select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id`, `model_has_roles`.`model_type` as `pivot_model_type` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = 1 and `model_has_roles`.`model_type` = 'App\Models\User'  

This is my controller

public function index(){
   $data=User::get();
   $info=Role::get();
   $permi=Permission::get();
   return view('admin.role',['userinfo'=>$data, 'roleinfo'=>$info,'per'=>$permi]);
}

This is my blade view where i retrieved user's information and assigned role to that user

@foreach ($userinfo as $users)
   <tr>
       <th scope="row">{{ $users->id }}</th>
       <td>{{ $users->name }}</td>
       <td>{{ $users->email }}</td>
       <td>
          @if(!empty($users->getRoleNames()))
              @foreach($users->getRoleNames() as $v)
                  <label class="badge badge-success">{{ $v }}</label>
              @endforeach
          @endif         
       </td>
   </tr>
@endforeach

I know I have to show a relation in the User model but I am confused about how to show a relation. Also, I have used spatie package and auth so my User model is extending Authenticatable. I am super confused about what to do.

Zia Yamin
  • 942
  • 2
  • 10
  • 34
Prabhu
  • 57
  • 1
  • 11
  • Does this answer your question? [Spatie Laravel permissions eager loaded](https://stackoverflow.com/questions/57478992/spatie-laravel-permissions-eager-loaded) – Kamlesh Paul Feb 09 '21 at 04:59
  • This question has been asked earlier and has a lot reference to refer. You can take a quick help. Stil if that does not help try leaving a comment with more details. https://stackoverflow.com/questions/60926026/laravel-spatie-get-all-permissions-with-eager-loading. – Jaswinder Singh Feb 09 '21 at 05:26
  • Thanks KamleshPaul and JaswinderSingh Those were helpful – Prabhu Feb 09 '21 at 05:40

1 Answers1

0

In User model I showed belongsToMany relation with model_has_roles table

function role(){
   return $this->belongsToMany(Role::Class, 'model_has_roles','model_id','role_id');
}

Then in my controller

public function index(){
    $data=User::with('role')->get();
    $info=Role::get();
    $permi=Permission::get();
    return view('admin.role',['userinfo'=>$data, 'roleinfo'=>$info,'per'=>$permi]);
}

Then in my blade view, i did little changes while fetching roles to assigned users

@foreach ($userinfo as $users)
    <tr>
       <th scope="row">{{ $users->id }}</th>
       <td>{{ $users->name }}</td>
       <td>{{ $users->email }}</td>
       <td>
          @if(!empty($users->role))
              @foreach($users->role as $v)
                 <label class="badge badge-success">{{ $v->name }}</label>
              @endforeach
           @endif         
        </td>
    </tr>
@endforeach

Then this reduced my query counts. Thank you to everyone who showed interest in this.

Zia Yamin
  • 942
  • 2
  • 10
  • 34
Prabhu
  • 57
  • 1
  • 11