【发布时间】:2020-03-19 13:40:15
【问题描述】:
我有以下型号
- 订单(ID、编号)
- 详细信息(id、item_id、count、user_id ...等)
- 项目(ID、名称)
- 猫(id,name)
- 在我的情况下不重要的用户 (id,name)
每个订单都有很多细节
每个细节都属于项目
每个项目都属于类别
每个细节都属于用户
这行代码是我写的
public function show(Order $order)
{
//
$orders= $order->details()->with(['item','user'])->get();
return response()->json(['details'=>$orders],200);
}
获得这样的响应:
"details": [
{
"id": 3089,
"count": 3,
"item_id": 102,
"order_id": 1,
"user_id": 10,
"created_at": "2019-11-22 22:19:23",
"updated_at": "2019-11-22 22:19:23",
"user": {
"id": 10,
"name": "Ms. Eda Stoltenberg DVM",
"username": "Brandy Murazik",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 5,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 102,
"code": "Lamar Hansen",
"cat_id": 91,
"created_at": "2019-11-22 22:15:54",
"updated_at": "2019-11-22 22:15:54"
}
},
{
"id": 3428,
"count": 1,
"item_id": 15,
"order_id": 1,
"user_id": 10,
"created_at": "2019-11-22 22:19:41",
"updated_at": "2019-11-22 22:19:41",
"user": {
"id": 10,
"name": "Ms. Eda Stoltenberg DVM",
"username": "Brandy Murazik",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 5,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 15,
"code": "Hilario Dicki",
"cat_id": 20,
"created_at": "2019-11-22 22:15:51",
"updated_at": "2019-11-22 22:15:51"
}
},
{
"id": 3493,
"count": 1,
"item_id": 129,
"order_id": 1,
"user_id": 6,
"created_at": "2019-11-22 22:19:45",
"updated_at": "2019-11-22 22:19:45",
"user": {
"id": 6,
"name": "Prof. Marina Kiehn",
"username": "Nickolas Hessel",
"verified": "0",
"verification_token": "mwWkL95jjyi5di9PSH3T3LXXZEE1W2DmegTxAOtN",
"status": "1",
"dept_id": 3,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 129,
"code": "Dr. Donnell Harber",
"cat_id": 54,
"created_at": "2019-11-22 22:15:55",
"updated_at": "2019-11-22 22:15:55"
}
},
{
"id": 4032,
"count": 1,
"item_id": 221,
"order_id": 1,
"user_id": 8,
"created_at": "2019-11-22 22:20:10",
"updated_at": "2019-11-22 22:20:10",
"user": {
"id": 8,
"name": "Prof. Thaddeus Boehm",
"username": "Frederick Kshlerin",
"verified": "1",
"verification_token": null,
"status": "1",
"dept_id": 3,
"created_at": "2019-11-22 22:15:47",
"updated_at": "2019-11-22 22:15:47"
},
"item": {
"id": 221,
"code": "Maia Hettinger V",
"cat_id": 57,
"created_at": "2019-11-22 22:15:58",
"updated_at": "2019-11-22 22:15:58"
}
},
{
"id": 4311,
"count": 1,
"item_id": 139,
"order_id": 1,
"user_id": 3,
"created_at": "2019-11-22 22:20:21",
"updated_at": "2019-11-22 22:20:21",
"user": {
"id": 3,
"name": "Cletus Walter I",
"username": "Johan Kemmer",
"verified": "0",
"verification_token": "M6MWe7mOmzcyM0rrlbMwVCX7q2vyULFKwSAJmQwl",
"status": "0",
"dept_id": 5,
"created_at": "2019-11-22 22:15:46",
"updated_at": "2019-11-22 22:15:46"
},
"item": {
"id": 139,
"code": "Tanner Schimmel",
"cat_id": 80,
"created_at": "2019-11-22 22:15:55",
"updated_at": "2019-11-22 22:15:55"
}
},
{
"id": 4821,
"count": 2,
"item_id": 243,
"order_id": 1,
"user_id": 1,
"created_at": "2019-11-22 22:20:41",
"updated_at": "2019-11-22 22:20:41",
"user": {
"id": 1,
"name": "Mrs. Fay Cassin",
"username": "Ryley Bode",
"verified": "0",
"verification_token": "E93hzJbIp2eCxbBGgtcsYDckRreASTPL6ZEAyyKP",
"status": "1",
"dept_id": 1,
"created_at": "2019-11-22 22:15:46",
"updated_at": "2019-11-22 22:15:46"
},
"item": {
"id": 243,
"code": "Miss Jaida Simonis DVM",
"cat_id": 53,
"created_at": "2019-11-22 22:15:59",
"updated_at": "2019-11-22 22:15:59"
}
}
]
但我还需要在 item 对象中获取 category 对象,这已经是关系是 Item 属于 Category 我知道我可以进行连接查询,但我更喜欢找到基于模型之间关系的最佳解决方案
如果可能的话,我该怎么做呢?
谢谢
【问题讨论】:
-
$orders= $order->details()->with(['item.category','user'])->get(); -
是的ssssss,工作就像一个魅力我发誓我已经尝试过,但我不知道为什么它不起作用哈哈哈你救了我
-
谢谢我的朋友
标签: php laravel eloquent laravel-query-builder