转换 laravel 中嵌套的 collection

Transform nested collection in laravel

我有一个我想要转换的嵌套 collection,将一些键“提升一个级别”并丢弃其他一些键。

collection 中的每件商品都含有过敏原 属性。

"allergens": [
        {
            "id": 2001,
            "info": "Allergy advice",
            "status": "does_contain",
            "product_id": 71576,
            "allergen_id": 1,
            "allergen": {
                "id": 1,
                "name": "Celery"
            }
        },
        {
            "id": 2002,
            "info": "Allergy advice",
            "status": "may_contain",
            "product_id": 71576,
            "allergen_id": 11,
            "allergen": {
                "id": 11,
                "name": "Peanuts"
            }
        }
    ],

我需要让每件物品的过敏原属性看起来像

"allergens": [
        {
            "id": 1,
            "name": "Celery"
            "status": "does_contain",
        },
        {
            "id": 11,
            "name": "Peanuts"
            "status": "does_contain",
        },
    ],

我试过以下方法:

$collection = $collection->transform(function ($item, $key) {
    $item->allergens = $item->allergens->map(function ($allergen) {
        return [
            'id' => $allergen->allergen->id,
            'name' => $allergen->allergen->name,
            'status' => $allergen->status,
        ];
    });
    
    return $item;
});

但它不会覆盖 allergens 属性

由于您将 collection 发布为 JSON,我对您的实际 collection 进行了逆向工程。事实证明,据我所知,您的 transform() 工作正常。也许这可以帮助您找到我和您的 collection 之间的差异,这可能会引导您找到 problem/solution:

$collection = collect([
  (object)[

    "allergens" => collect([
            (object)[
                "id" => 2001,
                "info" => "Allergy advice",
                "status" => "does_contain",
                "product_id" => 71576,
                "allergen_id" => 1,
                "allergen" => (object)[
                    "id" => 1,
                    "name" => "Celery"
                ]
            ],
            (object)[
                "id" => 2002,
                "info" => "Allergy advice",
                "status" => "may_contain",
                "product_id" => 71576,
                "allergen_id" => 11,
                "allergen" => (object)[
                    "id" => 11,
                    "name" => "Peanuts"
                ]
            ]
    ]),
  ]
]);
$collection = $collection->transform(function ($item, $key) {
    $item->allergens = $item->allergens->map(function ($allergen) {
        return [
            'id' => $allergen->allergen->id,
            'name' => $allergen->allergen->name,
            'status' => $allergen->status,
        ];
    });
    
    return $item;
});
dd($collection);

结果:

Illuminate\Support\Collection {#1779 ▼
  #items: array:1 [▼
    0 => {#1791 ▼
      +"allergens": Illuminate\Support\Collection {#1775 ▼
        #items: array:2 [▼
          0 => array:3 [▼
            "id" => 1
            "name" => "Celery"
            "status" => "does_contain"
          ]
          1 => array:3 [▼
            "id" => 11
            "name" => "Peanuts"
            "status" => "may_contain"
          ]
        ]
      }
    }
  ]
}