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

用 ORM 写无限极分类

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

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

直接上代码

模型



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

阅读量! (1557)

关于本站

昵称:无痕小Q

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

现居:北京

Email:1838638884@qq.com

    在无数个失眠的晚上,相信会有很多人,习惯性的闭上眼睛,安静的想念一个人,

网站公告

  • 欢迎来到我的博客

  • 1:欢迎来到我的博客


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


    3:博客会定期更新文章


    4:欢迎大家来捧场


    生活飞快地并非我一人。朱自清先生曾咏叹过时光匆匆:洗手的时候,日子从水盆里过去;吃饭的时候,日子从饭碗里过去;默默时,便从凝然的双眼前过去。

站点信息

甫昔少年日,早充观国宾。  读书破万卷,下笔如有神。 尚未佩妥剑,转眼便江湖。愿历尽千帆,归来仍少年。
  • 建站时间:2019-8-30
  • 网站程序:php,laravel-swoole框架
  • 今日流量:849(10分钟统计一次)
  • 本月流量:10550
  • 浏览总量:507017
  • 统计方式:中间件,redis消息队列,定时任务
    人生是一支歌,悠扬的旋律谱写出你春天的耕耘和秋天的收获;人生是一幅画,优美的线条勾画出你黎明的勤奋和傍晚的辉煌;人生是一首诗,优美的意境,书写着你失意的懊恼和成功的喜悦。