【发布时间】:2019-01-31 22:44:24
【问题描述】:
在我的数据库中,我有类别,它们可以有子类别。类别也有产品。例如:
Category
Sub-Category 1
Product 1
Sub-Sub-Category 1
Product 2
Product 3
类别模型
public function childs()
{
return $this->hasMany('App\Category', 'parent_id', 'id');
}
public function products()
{
return $this->belongsToMany('App\Products');
}
我的问题是,如果我做 $Category->products,我希望它给我所有的产品。如果我执行 $Sub-Sub-Category-1,它会给我产品 2 和产品 3。感谢您的帮助。
【问题讨论】:
-
没有关系可以做到这一点,您可能需要某种递归函数。
标签: php laravel relationship