【发布时间】:2021-12-20 18:07:39
【问题描述】:
我尝试通过 laravel-nestedset 来实现它
Laravel 8
例如:
类别模型:
public function parent()
{
$parent = $this->belongsTo('Category', 'parent_id');
return $parent;
}
public function children()
{
$children = $this->hasMany('Category', 'parent_id');
//$children->wherePublish(1);
return $children;
}
public function posts()
{
return $this->hasMany('Post');
}
我用一个例子测试了一下:
category1 //with 0 post in category1
category1 > category2 //with 10 posts in category2
category1 > category2 > category3 //with 5 posts in category3
我尝试测试它:
for //category1
$category->post()->get(); // return 0 post (I want 15 posts)
for //category2
$category->post()->get(); // return 10 posts (I want 15 posts)
for //category3
$category->post()->get(); // return 5 post (I want 5 posts)
【问题讨论】: