首页 > 解决方案 > 如何在我的数据库结构或任何其他解决方案上实现 hasManyThrough?

问题描述

我有口才关系的问题。
一共有三个表:

卖家:[id,name]
产品:[id,title,variety_type(nullable)]
库存:[id,sell_price,seller_id,color_id(nullable),size_id(nullable)]

颜色:[红色、黑色、蓝色]
尺寸:[s、m、l、xl、xxl]

 
卖家
标识名称
----------------------
1 第一位卖家
2 第二卖家

产品
Id Title 品种类型
-------------------------------------
1 个产品 1 个颜色


库存
Id Sell_price Seller_id color_id size_id
-------------------------------------------------- --------
1 10000 1 1
2 9800 1 2
3 10200 1 3
4 9900 2 1
5 9800 2 2
6 10000 2 3

事实上,每个产品都有很多卖家通过库存,如果我没记错的话,Laravel 中的 hasManyThrough 关系需要在我的数据库结构中不存在的卖家中的 inventory_id。现在我想在数据库中得到这个结果:

    id: 1,
    title: 'Product 1'
    sellers: [
        {
            id: 1,
            name: 'First Seller',
            inventories: [
                {
                    id: 1,
                    'Sell_price': 10000,
                    'color_id': 1
                },
                {
                    id: 2,
                    'Sell_price': 9800,
                    'color_id': 2
                },
                {
                    id: 3,
                    'Sell_price': 10200,
                    'color_id': 3
                },
            ]
        },
        {
            id: 1,
            name: 'Second Seller',
            inventories: [
                {
                    id: 4,
                    'Sell_price': 9900,
                    'color_id': 1
                },
                {
                    id: 5,
                    'Sell_price': 9800,
                    'color_id': 2
                },
                {
                    id: 6,
                    'Sell_price': 10000,
                    'color_id': 3
                },
            ]
        },
    ]
}

标签: laraveleloquentrelationship

解决方案


推荐阅读