Laravel框架之无限极分类
下图为我的表,其中pid为父级id,sort为排序
控制器调用模型:
/无限极分类/
public function showType(){
$model = BlueShop::ShowType();
return view('News/type',['typeList'=>$model]);//将分类结果返回给视图
}
模型部分代码:
/查询所有分类/
public static function showType(){
$info = DB::table('jy_type')->get();
$result = self::list_level($info,$pid=0,$level=0);
return $result;
}
/**
*书写一个调用无线分类的方法
*@param $level 分类级别
*@param $pid 父级id
*@param $data 所有分类
*/
public static function list_level($data,$pid,$level){
static $array = array();
foreach ($data as $k => $v) {
if($pid == $v->pid){
$v->level = $level;
$array[] = $v;
self::list_level($data,$v->id,$level+1);
}
}
return $array;
}
视图使用foreach渲染
<tr>
<th width="120" align="left">分类名称</th>
<th align="left">别名</th>
<th align="left">简单描述</th>
<th width="60" align="center">排序</th>
<th width="80" align="center">操作</th>
</tr>
@foreach($typeList as $k => $v)
<tr>
<td align="left">{{str_repeat('|-',$v->level)}}<a href="">{{$v->type_name}} </a></td>
<td>{{$v->alias}}</td>
<td>{{$v->description}}</td>
<td align="center">{{$v->sort}}</td>
<td align="center">
<a href="typeup?id={{$v->id}}">编辑</a> |
<a href="typedel?id={{$v->id}}">删除</a>
</td>
</tr>
@endforeach
渲染结果: