我正在使用刀片模板绘制树结构,但我想为任何级别的孩子编写它。我认为这可以使用递归来实现。这是代码
<div class="tree">
<ul>
<li>
<a href="#">{{ $root->first_name }} {{ $root->last_name }}</a>
@if($root->children->count())
<ul>
@foreach($root->children as $childlevel1)
<li>
<a href="#">{{ $childlevel1->first_name }} {{ $childlevel1->last_name }}</a>
@if($childlevel1->children->count())
<ul>
@foreach($childlevel1->children as $childlevel2)
<li>
<a href="#">{{ $childlevel2->first_name }} {{ $childlevel2->last_name }}</a>
@if($childlevel2->children->count())
<ul>
@foreach($childlevel2->children as $childlevel3)
<li>
<a href="#">{{ $childlevel3->first_name }} {{ $childlevel3->last_name }}</a>
</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif
</li>
</ul>
</div>
答案 0 :(得分:0)
我能够解决上述问题。我创建了一个部分刀片模板parentchild.blade.php并添加了以下代码:
@if($parent->children->count())
<ul>
@foreach($parent->children as $child)
<li>
<a href="#">{{ $child->first_name }} {{ $child->last_name }}</a>
@include('parentchild',['parent' => $child])
</li>
@endforeach
</ul>
@endif
然后我将上面的部分模板包含在主模板中,如下所示:
<div class="tree">
<ul>
<li>
<a href="#">{{ $root->first_name }} {{ $root->last_name }}</a>
@include('parentchild', ['parent' => $root] )
</li>
</ul>
所以,最后它的工作:)