PHP - "n" 列表中 children 的数量

PHP - "n" number of children in a list

这个问题是关于:

本质上,我拥有的是一系列 "Groups",每个组可以有任意数量的 child 组。所以它看起来像这样:

目前,我只能显示组和他们的 children,使用:

    <ul>
@foreach($contents as $content)
  <li>{{$content->title}}</li>
  @if($content->children->count() > 0)
    <ul>
      @foreach($content->children as $childContent)
        <li>{{$childContent->title}}</li>
      @endforeach
   </ul>
  @endif
@endforeach
</ul> 

如果我想添加更多的 children 层,我必须使用另一个 if 语句和 foreach 语句对此进行编码。显然,当一个组有 n^3,... children.

的数量时,这是不切实际的

是否有解决此问题的动态 while-loop 方法?任何帮助将不胜感激!!

您需要的是 递归部分 - 只要有更多组级别要显示,它就会自行加载。

// list_group.blade.php
<li>
  {{ $content->title }}
  @if($content->children->count() > 0)
    <ul>
      @foreach($content->children as $childContent)
        @include('list_group', array('content' => $childContent))
      @endforeach
    </ul>
  @endif
</li>

//in your template 
<ul>
  @foreach($contents as $content)
    @include('list_group', array('content' => $content))
  @endforeach
</ul>