21

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?

7
  • What is stored inside the first collection? It's difficult to answer without any code
    – ad4s
    Feb 27, 2017 at 20:30
  • I updated my question
    – user5523349
    Feb 27, 2017 at 20:33
  • Try to iterate the second collection and push each element onto the first collection. laravel.com/docs/5.4/collections#method-push
    – ad4s
    Feb 27, 2017 at 20:56
  • Do you need it to still be a collection? If not you can do this: array_merge($entries->all(), $posts->all()); Feb 27, 2017 at 21:46
  • @ad4s Hmm, it make sense, however it sounds a bit inefficient, what if i have more than 2 collections? is there any other alternative?
    – user5523349
    Feb 27, 2017 at 21:54

6 Answers 6

31

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.

5
  • this appear to be working, I'm sorry to add additional question, but what if I have 3 eloquent collections? let's say $entries, $posts and $users? do I need to call toBase() each time before merging another one? or just straight merge()?
    – user5523349
    Feb 28, 2017 at 16:35
  • 1
    I think you will have to call toBase() every time you want to merge into your current collection. But your best bet would be to try it out in a small prototype :)
    – Björn
    Feb 28, 2017 at 21:56
  • merge overwrites values in the old collection to values of the new collection, if the keys are the same. It is not, what was asked. toBase is not solving the problem, because all it does is makes a duplicate of the initial collection. Apr 15, 2020 at 5:28
  • I had to downvote, because concat is the right method for this case.
    – winkbrace
    Jul 2, 2020 at 10:07
  • The original question was for laravel 5.3 which doesn't have the concat method, I updated my answer with some more information about versions and a link to the concat answer.
    – Björn
    Jul 2, 2020 at 14:35
26

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);
1
  • 4
    This is the correct answer because it really appends. It does destroy the keys, but that should be the point, otherwise merge is the tool to pick.
    – winkbrace
    Jul 2, 2020 at 10:06
5

Here's the link: laravel7.X

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

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

$merged->all();
1

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

1
  • solves my problem, merging two different eloquent collections Oct 15, 2021 at 7:39
0

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

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

Laravel Collections: merge() method

3
  • I got an error Call to a member function getKey() on array
    – user5523349
    Feb 28, 2017 at 16:31
  • Aparently you have to turn the Illuminate\Database\Eloquent\Collection into a Illuminate\Support\Collection: something like this: collect($entries)->merge($posts->toArray()); more info here
    – Jeff
    Feb 28, 2017 at 23:40
  • You mean $entries->merge($posts->all());. toArray also changes your model attributes to an array.
    – Erin
    Jul 17, 2018 at 8:12
0

This is what worked for me using a loop:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy