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

用 ORM 写无限极分类

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

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

直接上代码

模型



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

阅读量! (1402)

关于本站

昵称:无痕小Q

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

现居:北京

Email:1838638884@qq.com

    洞角丹砂吐锦云,龟毛铅彩瑞氤氲。 我来收入县胎鼎,炼到洪蒙未剖分。

网站公告

  • 欢迎来到我的博客

  • 1:欢迎来到我的博客


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


    3:博客会定期更新文章


    4:欢迎大家来捧场


    咬定青山不放松,立根原在破岩中。 千磨万击还坚劲,任尔东西南北风。

站点信息

青山横北郭,白水绕东城。
此地一为别,孤蓬万里征。
浮云游子意,落日故人情。
挥手自兹去,萧萧班马鸣。 瑶池阿母绮窗开,黄竹歌声动地哀。 八骏日行三万里,穆王何事不重来。
  • 建站时间:2019-8-30
  • 网站程序:php,laravel-swoole框架
  • 今日流量:399(10分钟统计一次)
  • 本月流量:3161
  • 浏览总量:399302
  • 统计方式:中间件,redis消息队列,定时任务
    进军飞狐北,穷寇势将变。日落沙尘昏,背河更一战。 骍马黄金勒,雕弓白羽箭。射杀左贤王,归奏未央殿。 欲言塞下事,天子不召见。东出咸阳门,哀哀泪如霰。