Skip to content
Advertisement

Transform nested collection in laravel

I have a nested collection that I want to transform, pulling some keys “up a level” and discarding some other keys.

Every item in the collection has an allergens property.

JavaScript

I need to make each items allergens property, look like

JavaScript

I’ve tried the following:

JavaScript

But it doesn’t overwrite the allergens property

Advertisement

Answer

Since you’re posting your collection as JSON, I reverse engineered what your actual collection would look like. Turns out, your transform() works fine as far as I can tell. Maybe that helps you to find differences between my and your collection which might lead you to your problem/solution:

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement