I have 2 quite simple collections (The first has 2 values within the array products and the second is a simple array):
$ collection_1 = collect ([
'products' => [
['id' => 1, 'name' => 'Product 1'], ['id' => 2, 'name' => 'Product 2']
]
]);
$ collection_2 = collect ([
['id' => 4, 'name' => 'Product 4'], ['id' => 3, 'name' => 'Product 4']
]);
What I want to do is merge both collections using put (keeping the key products) but when using put I lose the values of the first collection and only get those of the second:
$collection_1->put('products', $collection_2);
Result:
{"products":[{"id":4,"name":"Product 4"},{"id":3,"name":"Product 4"}]}
How can I join both collections while maintaining the values both in the array "products"? This is the expected result:
{"products":[{"id":1,"name":"Product 1"},{"id":2,"name":"Product 2"},{"id":4,"name":"Product 4"},{"id":3,"name":"Product 4"}]}
Thanks