在上一篇文章中[php中使用递归显示层级结构](https://www.yuanchengzhushou.cn/article/8074.html)中我们已经会了如何用原生的php来显示层级
那么如何在`laravel`中实现呢?
只需在`controller`中加入以下代码即可
```
function showCategoryTree($categories, $n) {
if (isset($categories[$n])) {
foreach ($categories[$n] as $category) {
echo str_repeat('|-', $category['level']) . $category['categoryName'] ."
";
$this->showCategoryTree($categories, $category['id']);
}
}
return;
}
public function get(Request $request){
$list = Categories::orderBy('parentCategory')->orderBy('sortInd')->get();
$categories = [];
foreach ($list->toArray() as $category) {
$categories[$category['parentCategory']][] = $category;
}
$this->showCategoryTree($categories, 0);
}
```