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

用 ORM 写无限极分类

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

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

直接上代码

模型



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);
}

阅读量! (1627)

关于本站

昵称:无痕小Q

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

现居:北京

Email:1838638884@qq.com

    辛苦遭逢起一经,干戈寥落四周星。山河破碎风飘絮,身世浮沉雨打萍。惶恐滩头说惶恐,零丁洋里叹零丁。人生自古谁无死?留取丹心照汗青。

网站公告

  • 欢迎来到我的博客

  • 1:欢迎来到我的博客


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


    3:博客会定期更新文章


    4:欢迎大家来捧场


    生活的道路一旦选定,就要勇敢地走到底,决不回头。

站点信息

泉眼无声惜细流,树阴照水爱晴柔。 小荷才露尖尖角,早有蜻蜓立上头。 空碧无云露湿衣,众星光外涌清规。 东林莫碍渐高势,四海正看当路时。 还许分明吟皓魄,肯教幽暗取丹枝。 可怜关夜婵娟影,正对五候残酒卮。
  • 建站时间:2019-8-30
  • 网站程序:php,laravel-swoole框架
  • 今日流量:173(10分钟统计一次)
  • 本月流量:28506
  • 浏览总量:554689
  • 统计方式:中间件,redis消息队列,定时任务
    寒雨连江夜入吴,平明送客楚山孤。
洛阳亲友如相问,一片冰心在玉壶。