laravel层级结构展示

在上一篇文章中[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);
}
```

    A+
发布日期:2021年01月15日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: