【发布时间】:2019-09-30 07:43:20
【问题描述】:
我有像这样与自身相关的模型
public function children() {
return $this->hasMany(AppointmentPart::class,'parent_id');
}
我想急切加载此模型的查询,但因为每一行都可以有子 with() 方法对我不起作用
$parts = AppointmentPart::whereParentId(0)->with('children')->get();
我的刀:
<ul id="treeData">
@foreach($parts as $part)
<li id="{{ $part->id }}"
@if(!empty($part->children)) class="folder" @endif>
{{ $part->title }}
@if(!empty($part->children))
@include('appointment::admin.appointment.layout._part_child', array('parts' => $part->children))
@endif
</li>
@endforeach
</ul>
_part_child 的内容:
<ul>
@foreach($parts as $part)
<li id="{{ $part->id }}" @if(!empty($part->children)) class="folder" @endif>
{{ $part->title }}
@include('appointment::admin.appointment.layout._part_child',['parts'=>$part->children])
</li>
@endforeach
</ul>
我该怎么做?
【问题讨论】:
-
不太清楚您要实现的目标...您能否在使用
with()方法的地方也添加代码?
标签: php laravel eloquent eager-loading laravel-5.8