I have the model that has relation to itself like this
public function children() {
return $this->hasMany(AppointmentPart::class,'parent_id');
}
I want to eager loading for queries for this model but because every row can have child with() method doesn't work for me
$parts = AppointmentPart::whereParentId(0)->with('children')->get();
my blade:
<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>
content of _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>
how can I do this?
with()
method? – Massacre