【发布时间】:2019-02-23 05:01:35
【问题描述】:
表一:categories: id,name,parent_id
id name parent_id
1 Vehicle null
2 Car 1
3 Sedan 2
Vehicle > Car > Sedan
表2:features: id, name
id name
1 type
2 cylinder
3 color
4 weight
表 3:category_feature: category_id, feature_id
category_id feature_id
1 1
1 2
2 3
3 4
我可以按父级(类别)获取所有功能。
例如:
类别型号:
public function features()
{
return $this->belongsToMany(Feature::class);
}
还有:
$category = Category::find(1);
$features = $category->features()->get();
如何按子类别获取 孩子的所有特征和父亲类别的特征?
类似这样的:
$category = Category::find(3);
$features = $category->parent_features()->get();
我希望它返回这些:类型、圆柱体、颜色、重量
【问题讨论】:
-
如何``` $category = Category::find(3); $features = $category::with('hasParent')->features()->get(); ``` 假设您在类别中设置了关系以检查它是否有父对象。
标签: laravel eloquent many-to-many