//实现无限级分类
public functiongetTree(){ $categorys = Category::all();
return $this->makeTree($categorys, 'cate_id', 'cate_pid', 'cate_name', 0);
}
public functionmakeTree($list,$pk='id',$pid='pid',$child='child',$root=0){
$tree = array();
foreach ($list as $key => $val) {
if ($val[$pid] == $root) {
//获取当前$pid所有子类 unset($list[$key]);
if (!empty($list)) {
$tmpChild = self::makeTree($list, $pk, $pid, $child, $val[$pk]);
if (!empty($tmpChild)) {
$val['_' . $child] = $tmpChild;
}
}
$tree[] = $val;
}
}
return $tree;
}