繁体   English   中英

如何使用 group by 修改嵌套的 Laravel 集合

[英]How to modify nested Laravel collection using group by

我想使用 group by 修改嵌套集合。

这是样本集

    "document": [
        {
            "id": 1,
            "company_id": 4,
            "client_id": 1,
            "status": 1,
            "client": {
                "id": 1,
                "company_id": 4,
                "name": "1663159185735-client"
            },
           "document_items": [
                {
                    "id": 1,
                    "master_id": 5,
                    "text_value": "piyo",
                },
                {
                    "id": 2,
                    "master_id": 5,
                    "text_value": "fuga",
                },
                {
                    "id": 3,
                    "master_id": 3,
                    "text_value": "hoge",
                }
            ]
        }
    ]

我想这样改变。

    "document": [
        {
            "id": 1,
            "company_id": 4,
            "client_id": 1,
            "status": 1,
            "client": {
                "id": 1,
                "company_id": 4,
                "name": "1663159185735-client"
            },
           "document_items": [
                5: [{
                    "id": 1,
                    "master_id": 5,
                    "text_value": "piyo",
                   },
                   {
                    "id": 2,
                    "master_id": 5,
                    "text_value": "fuga",
                   }
                ],
                3: [{
                    "id": 2,
                    "master_id": 5,
                    "text_value": "fuga",
                       }
                  ]
            ]
        }
    ]

我尝试写下面的代码;

        $result->map(function ($v){
            
            $v->documentItems = $v->documentItems->groupBy('master_id');
            return  $v;
        });

但 output 键是documentItems而不是document_items

我改为

 $v->document_items = $v->documentItems->groupBy('master_id');

关键是 drawing_drawing_items 但不是 groupby(简单数组)

如何修改分组并保留关键案例?

I changed to $v->document_items = $v->documentItems->groupBy('master_id'); key is drawing_drawing_items but not groupby(simple array)

你也应该像这样改变$v->document_items->groupBy('master_id')

关键是“document_items”而不是“documentItems”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM