Level 14
I see that there is no update() method for Collections so that didn't work...
I have this Cars_And_Parts collection that contains both cars and car parts. I would like to take all the car parts and add each to the correct car's car_parts array. I can't seem to figure out how to do this using Collection methods.
Here is my original $Cars_And_Parts Collection
[
{
"id": 1,
"description": "Tesla Model X",
"car_parts": [],
},
{
"id": 2,
"description": "Nissan Leaf",
"car_parts": [],
},
{
"id": 3,
"description": "Tesla Engine",
"is_car_part": true,
"car_id": 1,
},
{
"id": 4,
"description": "Nissan battery",
"is_car_part": true,
"car_id": 2,
},
{
"id": 5,
"description": "Nissan lights",
"is_car_part": true,
"car_id": 2,
},
]
And after adding all the parts it would look like this..
[
{
"id": 1,
"description": "Tesla Model X",
"car_parts": [
{
"id": 3,
"description": "Tesla Engine",
"is_car_part": true,
"car_id": 1,
}
],
},
{
"id": 2,
"description": "Nissan Leaf",
"car_parts": [
{
"id": 4,
"description": "Nissan battery",
"is_car_part": true,
"car_id": 2,
},
{
"id": 5,
"description": "Nissan lights",
"is_car_part": true,
"car_id": 2,
},
],
},
{
"id": 3,
"description": "Tesla Engine",
"is_car_part": true,
"car_id": 1,
},
{
"id": 4,
"description": "Nissan battery",
"is_car_part": true,
"car_id": 2,
},
{
"id": 5,
"description": "Nissan lights",
"is_car_part": true,
"car_id": 2,
},
]
Please or to participate in this conversation.