Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用“获取复杂集合”;加上;laravel中的深层关系模型方法_Php_Laravel_Eloquent_Laravel Query Builder - Fatal编程技术网

Php 使用“获取复杂集合”;加上;laravel中的深层关系模型方法

Php 使用“获取复杂集合”;加上;laravel中的深层关系模型方法,php,laravel,eloquent,laravel-query-builder,Php,Laravel,Eloquent,Laravel Query Builder,我有以下型号 订单(id、编号) 详细信息(id、项目id、计数、用户id…等) 项目(id、名称) 猫(身份证、姓名) 用户(id,name),这在我的情况下并不重要 每个订单都有很多细节 每个细节都属于项目 每个项目都属于一个类别 每个细节都属于用户 我写了这行代码 public function show(Order $order) { // $orders= $order->details()->with(['item','user'])->ge

我有以下型号

  • 订单(id、编号)
  • 详细信息(id、项目id、计数、用户id…等)
  • 项目(id、名称)
  • 猫(身份证、姓名)
  • 用户(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()是的,很有魅力我发誓我已经试过了,但我不知道为什么不起作用哈哈哈,你救了我,我的朋友很高兴再次见到你
$orders = $order->details()->with(['item.category', 'user'])->get();