码迷,mamicode.com
首页 > 其他好文 > 详细

无限极分类

时间:2019-10-04 22:53:19      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:extends   enc   end   _id   分类   bsp   nbsp   private   this   

 

class CategoryController extends Controller
{
    public function index()
    {
        $rootCats = Category::where(parent_id, null)->get();
        return $this->tree($rootCats);
    }

    private function tree($cats) {
        $tree = [];
        foreach ($cats as $cat) {
            $childrenCats = Category::where(parent_id, $cat->id)->get();
            if ($childrenCats->isNotEmpty()) {
                $cat->children = $this->tree($childrenCats);
            }
            $tree[] = $cat;
        }
        return $tree;
    }
}

 

无限极分类

标签:extends   enc   end   _id   分类   bsp   nbsp   private   this   

原文地址:https://www.cnblogs.com/fenle/p/11623450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!