分享一个用 ORM 写无限极分类的帖子, 大佬请跳过.
实现无限极分类,运用场景为:树目录/部门分类/动态菜单等
直接上代码:
模型:
protected $table = 'menu';//自定义表名(protected $table)
protected $primaryKey = 'id';//主键字段,默认为id
public $timestamps = false;//如果数据表中没有created_at和updated_id字段,则$timestamps则可以不设置,默认为true
public function childrenModule()
{
return $this->hasMany(Menu::class,'fatherId','id');
}
public function children()
{
return $this->childrenModule()->with('children');
}
控制层:
$data = Menu::where(['fatherId' => 0, 'isDel' => 0])
->select('id', 'title', 'href', 'fontFamily', 'icon', 'spread', 'isCheck')
->with(['children:id,fatherId,title,href,fontFamily,icon,spread'])
->get();
最终实现效果:
{
"id": 10,
"title": "框架管理",
"href": "sys/pages/frame/index",
"fontFamily": "ok-icon",
"icon": "",
"spread": 0,
"isCheck": 0,
"children": [
{
"id": 11,
"fatherId": 10,
"title": "路由管理",
"href": "",
"fontFamily": "ok-icon",
"icon": "",
"spread": 0,
"children": [
{
"id": 12,
"fatherId": 11,
"title": "路由管理1",
"href": null,
"fontFamily": "ok-icon",
"icon": "",
"spread": 0,
"isCheck": 0,
"isDel": 1,
"children": []
},
{
"id": 13,
"fatherId": 11,
"title": "路由管理2",
"href": null,
"fontFamily": "ok-icon",
"icon": "",
"spread": 0,
"isCheck": 0,
"isDel": 1,
"children": []
}
]
}
]
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
不得不说,,laravel真的是非常优雅
有一层就多查询一次
如果
menu
表数据不多,用DB全部查询出来 存入数组再用数组来排序父子关系
https://github.com/lyxxg/lartree
我之前也写过一个包,后面发现没啥用,需求不一致。
总是在此基础上修改。
分类的话可以看看预排序这个
kalnoy/nestedset
包@ct4477xx_join
左右值算法:https://github.com/lazychaser/laravel-nest...
另一种算法:https://github.com/jiaxincui/closure-table
集合直接转无限级:https://github.com/TypiCMS/NestableCollect...
前两种适合量大点的,都需要动数据库,第三种不需要动数据库,适合菜单啥的这种
直接给代码 我喜欢这样搞
都是大佬,,,,活到老 学到老...