在上一篇文章中php中使用递归显示层级结构中我们已经会了如何用原生的php来显示层级
那么如何在laravel
中实现呢?
只需在controller
中加入以下代码即可
function showCategoryTree($categories, $n) {
if (isset($categories[$n])) {
foreach ($categories[$n] as $category) {
echo str_repeat('|-', $category['level']) . $category['categoryName'] ."<br>";
$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);
}
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
转载请注明:laravel层级结构展示 出自老鄢博客 | 欢迎分享