user5523349
user5523349

Reputation:

Append a Laravel collection with another collection

I am trying to append an Eloquent collection with another Eloquent collection in Laravel 5.3.

This is what I've done so far:

$entries = Entry::all();
$posts   = Post::all();

$entries->merge($posts);

I tried to use merge() as shown in the code above, but it seems that I'm stuck with this kind of problem (since some of them have the same id with the same value):

Collection merge eating up some rows

Any ideas?

Upvotes: 25

Views: 63032

Answers (7)

ntheorist
ntheorist

Reputation: 109

concat() and merge() both work, with the one caveat that they don't actually modify the original collection, but instead return a new one with the appended items. I often like having a single collection variable and being able to append an array of items to it without having to constantly reassign it, so I wrote a macro to do this, which you can put in your application's service provider.

Collection::macro('append', function( $value ){
    if( empty($value) ) return;
    $value = Arr::wrap($value);
    foreach($value as $item) $this->push($item);
});

then you can do

$items = collect([1,2,3]);
$more = [4,5,6];
$items->append($more);
dump($items->toArray());
// [1,2,3,4,5,6]

Upvotes: 0

Codeparl
Codeparl

Reputation: 326

This is what worked for me using a loop:

$collection= collect([]);
 foreach ($otherCollection as  $other)
 $collection= $collection->merge($other);
   

Upvotes: 0

Björn
Björn

Reputation: 5876

For versions < 5.4 you can merge the two eloquent collections by resetting keys with toBase like this:

$mergedCollection = $entries->toBase()->merge($posts);

For versions >= 5.4 you can use concat as suggested by Jason.

Upvotes: 36

Jason
Jason

Reputation: 4772

I believe you may be looking for concat(). This will append one container to the end of another container, regardless of the keys of either.

$mergedCollection = $entries->concat($posts);

Upvotes: 29

Ray Zion
Ray Zion

Reputation: 678

Here's the link: laravel7.X

$collection = collect(['Desk', 'Chair']);

$merged = $collection->merge(['Bookcase', 'Door']);

$merged->all();

Upvotes: 5

Yevgeniy Afanasyev
Yevgeniy Afanasyev

Reputation: 41270

if you need to merge big_products and small_products:

$products = $bigProducts->values()->merge($smallProducts->values());

The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection.

But

If the given items's keys are numeric, the values will be appended to the end of the collection:

Thus, all you need is to get rid of keys and you can do it with ->values() function.

Tested on Laravel-6

Upvotes: 1

Jeff
Jeff

Reputation: 166

The merge() method receives an array, so you have to do something like

$entries->merge($posts->toArray());

Laravel Collections: merge() method

Upvotes: 0

Related Questions