其他分享
首页 > 其他分享> > 无限极分类

无限极分类

作者:互联网

 

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;
    }
}

 

标签:Category,get,分类,tree,cat,无限极,id,childrenCats
来源: https://www.cnblogs.com/fenle/p/11623450.html