首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >雄辩地从中间表中获取表

雄辩地从中间表中获取表
EN

Stack Overflow用户
提问于 2018-07-11 16:03:42
回答 1查看 32关注 0票数 0

下面给出了3个表

代码语言:javascript
运行
复制
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

我希望立即加载产品和属性组,并使用其属性加载属性组

EN

回答 1

Stack Overflow用户

发布于 2018-07-11 22:28:31

您可以使用BelongsToMany关系:

代码语言:javascript
运行
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51280245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档