Laravel急切的加载似乎不起作用
请参见下图中的数据库关系。 我想显示教室的所有可用小时数(每天9小时),包括该小时的容量和该小时允许的部门,包括排除在教室之外的科目或教室专用的科目。 在show.blade.php中,我构建了一个表,每个表单元格显示一个可用小时。因为这需要两个循环,一个用于白天,一个用于小时,所以我想用关系加载教室。但从完成请求所需的时间(近2秒)和望远镜报告的查询数量来看,无论是否有紧急加载,数据库都会受到质疑 在我的控制器中,我加载如下关系:Laravel急切的加载似乎不起作用,laravel,eager-loading,Laravel,Eager Loading,请参见下图中的数据库关系。 我想显示教室的所有可用小时数(每天9小时),包括该小时的容量和该小时允许的部门,包括排除在教室之外的科目或教室专用的科目。 在show.blade.php中,我构建了一个表,每个表单元格显示一个可用小时。因为这需要两个循环,一个用于白天,一个用于小时,所以我想用关系加载教室。但从完成请求所需的时间(近2秒)和望远镜报告的查询数量来看,无论是否有紧急加载,数据库都会受到质疑 在我的控制器中,我加载如下关系: $classroom->load('available
$classroom->load('availableHours.allowedDepartments.exclusiveSubjects.subject',
'availableHours.allowedDepartments.excludedSubjects.subject',
'availableHours.allowedDepartments.department');
<!-- loop through all the hours of the day -->
@for($i=1;$i<=$maxHour;$i++)
<tr>
<td>
<a href="/classrooms/{{$classroom->number}}/0/{{$i}}/edit">
{{$i}}
</a></td>
<!-- loop through all the days -->
@foreach($days as $day)
<!-- show the available hour either in green (available) or red (unavailable)-->
<td>
<a href="/classrooms/{{$classroom->number}}/{{$day->id}}/{{$i}}/edit">
<!-- show the capacity of the hour of the day-->
{{ $classroom->availableHours()->where([['day_id','=',$day->id],['hour_of_day',$i]] )->first()->capacity }} <br/>
@foreach($classroom->availableHours()->where([['day_id','=',$day->id],['hour_of_day',$i]] )->first()->allowedDepartments as $allowedDepartment)
<!-- show the permitted departments along with any exclusive or excluded subjects-->
{{$allowedDepartment->department->name}}
@foreach ($allowedDepartment->exclusiveSubjects as $subjectDepartment)
{{$subjectDepartment->subject->code}}
@endforeach
<br/>
@endforeach
</a></td>
@endforeach
</tr>
@endfor
如果我扔掉教室,关系就会被加载。在我的show.blade.php中,我循环使用可用的小时,如下所示:
$classroom->load('availableHours.allowedDepartments.exclusiveSubjects.subject',
'availableHours.allowedDepartments.excludedSubjects.subject',
'availableHours.allowedDepartments.department');
<!-- loop through all the hours of the day -->
@for($i=1;$i<=$maxHour;$i++)
<tr>
<td>
<a href="/classrooms/{{$classroom->number}}/0/{{$i}}/edit">
{{$i}}
</a></td>
<!-- loop through all the days -->
@foreach($days as $day)
<!-- show the available hour either in green (available) or red (unavailable)-->
<td>
<a href="/classrooms/{{$classroom->number}}/{{$day->id}}/{{$i}}/edit">
<!-- show the capacity of the hour of the day-->
{{ $classroom->availableHours()->where([['day_id','=',$day->id],['hour_of_day',$i]] )->first()->capacity }} <br/>
@foreach($classroom->availableHours()->where([['day_id','=',$day->id],['hour_of_day',$i]] )->first()->allowedDepartments as $allowedDepartment)
<!-- show the permitted departments along with any exclusive or excluded subjects-->
{{$allowedDepartment->department->name}}
@foreach ($allowedDepartment->exclusiveSubjects as $subjectDepartment)
{{$subjectDepartment->subject->code}}
@endforeach
<br/>
@endforeach
</a></td>
@endforeach
</tr>
@endfor
@对于($i=1;$i)
@foreach($天作为$天)
当您调用$choork->availableHours()
时,实际上是再次查询关系函数并返回它,而不是访问已经存在的函数
在控制器中加载关系后,对象将被包含所需值的属性填充。因此,在加载后,请改用$kitcher->availableHours
。(与$allowedDepartment->exclusiveSubjects类似)