laravel无限级分类

数据库:
laravel无限级分类_第1张图片

遍历函数:

public function tree($parentId = 0)
    {
        $rows = Category::where('parentId', $parentId)->get()->toArray();
        $arr = array();

        if (sizeof($rows) != 0){
            foreach ($rows as $key => $val){
                $val['list'] = $this->tree($val['id']);
                $arr[] = $val;
            }
            return $arr;
        }

    }

你可能感兴趣的:(laravel,php)