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

用 ORM 写无限极分类

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

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

直接上代码

模型



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

阅读量! (1346)

关于本站

昵称:无痕小Q

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

现居:北京

Email:1838638884@qq.com

点击排行

细草微风岸,危樯独夜舟。  星垂平野阔,月涌大江流。  名岂文章著,官应老病休。  飘飘何所似?天地一沙鸥。 细草微风岸,危樯独夜舟。 星垂平野阔,月涌大江流。 名岂文章著,官应老病休。 飘飘何所似?天地一沙鸥。
    欲下星斗动,天乐一声肌骨寒。  金霞昕昕渐东上,轮欹影促犹频望。  绝景良时难再并,他年此日应惆怅。

网站公告

  • 欢迎来到我的博客

  • 1:欢迎来到我的博客


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


    3:博客会定期更新文章


    4:欢迎大家来捧场


    尝同徐勉构东田,花竹成阴雨后天。为爱宪台宽白简,得随相府赏红莲。清樽屡釂吟情逸,红袖频翻舞态妍。归兴直须三鼓尽,月华况是十分圆。

站点信息

渭城朝雨浥轻尘,客舍青青柳色新。
劝君更尽一杯酒,西出阳关无故人。 海纳百川,有容乃大;壁立千仞,无欲则刚。
  • 建站时间:2019-8-30
  • 网站程序:php,laravel-swoole框架
  • 今日流量:0(10分钟统计一次)
  • 本月流量:0
  • 浏览总量:396141
  • 统计方式:中间件,redis消息队列,定时任务
    听闻远方有你,动身跋涉千里,我吹过你吹过的风,这算不算相拥,我喜欢你,从一而终,认真且怂。