laravel - 如何在我的数据库结构或任何其他解决方案上实现 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
},
]
},
]
}
解决方案
推荐阅读
- xamarin - 从 Xamarin 跨平台连接到 Oracle 数据库
- c# - 在 Xamarin 表单和 ASP.NET MVC 5 (.NET 4.6.1) 项目之间共享代码
- java - 带有完整构造函数调用的方法引用作为 Java 中的 lambda 表达式
- c++ - 使用 set 方法时,protobuf 成员的地址会改变吗?
- macos - mac mini 进入睡眠状态后如何让 ngrok 保持活力
- c - 如何在 Windows 10 上安装 dr memory
- sql-server - 出现错误:MERGE 语句在 MERGE 语句中指定的某些(但不是全部)操作上具有 INSTEAD OF 触发器
- php - 当日志显示已将请求重写为 php 文件时,Apache 仍会加载测试页面
- angular - Angular 7 - 通过 url 导航到动态路由
- mapbox-gl-js - 如何在 HTML 页面中嵌入 kepler.gl 地图?