您现在的位置是:首页> 编程文章 个人博客模板

用 ORM 写无限极分类

无痕小Q个人博客 2020-04-12 02:04:12主页 1374人已围观

简介实现无限极分类,运用场景为:树目录 / 部门分类 / 动态菜单等

直接上代码

模型



namespace App;
use Illuminate\Database\Eloquent\Model;
class AdminMenu extends Model
{
// 菜单表
protected $table = 'admin_menu';
protected $fillable = ['parent_id','order','title','icon','uri','permission'];

public function childCategory() {
return $this->hasMany('App\AdminMenu', 'parent_id', 'id')
->orderBy('order', 'asc');
}

public function all_list()
{
return $this->childCategory()->with('all_list');
}
}
protected $table = 'admin_menu';
protected $fillable = ['parent_id','order','title','icon','uri','permission'];
public function childCategory() {
return $this->hasMany('App\AdminMenu', 'parent_id', 'id')
->orderBy('order', 'asc');
}
public function all_list()
{
return $this->childCategory()->with('all_list');
}
}

使用

// 列表
public function list(){
$data = AdminMenu::with('all_list')
->where('parent_id',0)
->orderBy('order', 'asc')
->get();
return response()->success($data);
}

阅读量! (1374)

关于本站

昵称:无痕小Q

职业:php-go-web开发工程师

现居:北京

Email:1838638884@qq.com

    国破山河在,城春草木深。  感时花溅泪,恨别鸟惊心。  烽火连三月,家书抵万金。  白头搔更短,浑欲不胜簪。

网站公告

  • 欢迎来到我的博客

  • 1:欢迎来到我的博客


    2:博客免费api接口现已上线


    3:博客会定期更新文章


    4:欢迎大家来捧场


    剑外忽传收蓟北,初闻涕泪满衣裳。  却看妻子愁何在,漫卷诗书喜欲狂。  白日放歌须纵酒,青春作伴好还乡。  即从巴峡穿巫峡,便下襄阳向洛阳。

站点信息

楼上残灯伴晓霜,独眠人起合欢床。  相思一夜情多少?地角天涯不是长。 有些路很远,走下去会很累。可是,不走,会后悔。
  • 建站时间:2019-8-30
  • 网站程序:php,laravel-swoole框架
  • 今日流量:0(10分钟统计一次)
  • 本月流量:0
  • 浏览总量:396141
  • 统计方式:中间件,redis消息队列,定时任务
    转缺霜输上转迟, 好风偏似送佳期。  帘斜树隔情无限, 烛暗香残坐不辞。  最爱笙调闻北里, 渐看星潆失南箕。  何人为校清凉力, 欲减初圆及午时。