I have a task manager and each user can be assigned task. I have these 4 databases (I'm only showing the columns needed for this discussion)
users:
--------------------------
| id | username |
---------------------------
| 1 | peterg |
----------------------------
| 2 | louisg |
----------------------------
tasks:
-----------------------------------------------------
| id | title | project_id |
------------------------------------------------------
| 1 | Finsih hw | 1 |
---------------------------------------------------
| 2 | cut grass | 4 |
----------------------------------------------------
a pivot table for users and tasks user_task:
----------------------------------------------
| id | task_id | user_id |
---------------------------------------------
| 1 | 1 | 1 |
--------------------------------------------
| 2 | 1 | 2 |
---------------------------------------------
| 1 | 2 | 2 |
--------------------------------------------
| 1 | 2 | 4 |
--------------------------------------------
and finally my projects table:
---------------------------
| id | title |
-----------------------------
| 1 | first_title |
---------------------------
| 2 | second_title |
------------------------------
here is the user relationship with tasks
public function assignedTasks()
{
return $this->belongsToMany('RL\Tasks\Task', 'user_task')
->orderBy('priority', 'ASC')
->orderBY('created_at', 'DESC')
->where('stage', '=', 1);
}
My Laravel route to show this page is /{username}/assigned-task
so when this route is hit a have a service class that fetches all the task for that user
public function assignedTasks($username)
{
$user = $this->member->getUserIdByUsername($username);
if(!$user) throw new UsernameNotFoundException;
$tasks = $user->assignedTasks;
return $tasks;
}
my html to dislay the assigned tasks:
<div id="assignments">
@foreach(array_chunk($tasks->all(), 3) as $row)
<div class="row">
<?php $task_id = [];?>
@foreach($row as $task)
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{$task->title}}.</h3>
</div>
<div class="panel-body">
<p>{{$task->description}}</p>
<p><strong>Project: </strong>{{$task->project->title}}</p>
<p class=""><strong>Priority: </strong>{{$task->priority}}</p>
<p class="right"><strong>Stage: </strong>{{$task->stage}}</p>
<a href="">Details</a>
</div>
</div>
</div>
@endforeach
</div>
@endforeach
</div>
This works fine but when I check the SQL I noticed that for each time i try to {{$task->project->title}}
a select * from project is run so if i have 50 assigned tasks thats 50 times that sql runs. How can i eager load projects in a many to many relationship?
You have to use eager loading in your getUserIdByUsername($username);
method.
For example it will looks like
User::with('tasks')->where('username',$username)->get();
Now when you wil call $tasks = $user->assignedTasks;
it will fetch data only once.
Read Docs Eager Loading
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.