martinszeltins's avatar

How to add collection items to another item?

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,
    },
]
0 likes
5 replies
martinszeltins's avatar

I see that there is no update() method for Collections so that didn't work...

martinszeltins's avatar

I ended up doing something like this...

$parts = $cars_and_parts->where('is_car_part', 1);

$cars_and_parts->each(function($item) use ($parts) {
    $parts_list = $parts->where('car_id', $item->id);

    if (count($parts_list) > 0) {
        $item->car_parts = $parts_list->values();
    }
});
abdulaziz's avatar

try this

foreach($Cars_And_Parts as $part ){
	if( array_key_exists("car_id", $part) ){
		foreach($Cars_And_Parts as $car ){
			if( $part->car_id == $car->id ){
				$car->car_parts->push($part);
			}
		}
	}
}
Ola-Yusuf's avatar

This works perfectly.

       $newCarsAndParts = $Cars_And_Parts->map(function ($item) use ($Cars_And_Parts) {
                foreach ($Cars_And_Parts as $key => $value) {
                    if(array_key_exists('is_car_part', $value)) 
                        if( $item['id'] === $value['car_id'])
                            {
                            empty($item['car_parts'])?
                            $item['car_parts'][0] = $value :
                            $item['car_parts'][count($item['car_parts'])] = $value;
                            }
                }
                return $item;
            });

Please or to participate in this conversation.