下面给出了3个表
Products
+id
+Name
ProductAttributes
+id
+name
+value
+product_id
+attribute_group_id
AttributeGroups
+id
+name
relation
---------
Product hasmany ProductAtrribute
ProductAtrributes belongsTo Products
ProductAtrributes belongsTo AttributeGroups我希望立即加载产品和属性组,并使用其属性加载属性组
发布于 2018-07-11 22:28:31
您可以使用BelongsToMany关系:
class Product extends Model {
public function attributeGroups() {
return $this->belongsToMany(AttributeGroup::class, 'product_attributes')
->withPivot('name', 'value');
}
}
$product = Product::find(1);
foreach($product->attributeGroups as $attributeGroup) {
// $attributeGroup->name
// $attributeGroup->pivot->value
}https://stackoverflow.com/questions/51280245
复制相似问题