Laravel merge collections failing?

Related searches

I'm trying to merge several collections but I'm not able to do this for any reason. My code looks like that:

$restaurant = \App\Restaurant::find(3);
$images     = $restaurant->images;
$userimages = $restaurant->userimages;
$hours      = $restaurant->hours;

$collectionMerge1 = $images->merge($userimages)->merge($hours);
$collectionMerge2 = $restaurant->userimages->merge($hours)->merge($images);
$collectionMerge3 = collect(array_merge($images->all(), $userimages->all(), $hours->all()));

The only merge that works is the last one. The previous ones are not showing all items that the collections contains. Why is this? What am I doing wrong? If you take a look to the picture you will see that there are missing objects in merges 1 and 2. I've tried several ways to do that, but only works the last one. Maybe the merge method is having issues with keys?

In general i feel there is a lot of pitfalls with merge, i think (in your case) most of the problems arises when you somehow tries to merge key value arrays together or concat indexed arrays. My solution for this problem, is to use concat instead of merging. Concat adds another collection or array to the end of the current. So you will not get weird cases, as shown here.


Quick fixes for weird issues: Laravel Eloquent collections merge , Laravel collections is a powerful tool to process data, but it comes with Use push() method instead or rethink your approach to the problem to� {note} Methods that mutate the collection (such as shift, pop, prepend etc.) are not available on the LazyCollection class.. Lazy Collection Methods. In addition to the methods defined in the Enumerable contract, the LazyCollection class contains the following methods:

This is old, but for posterity.

Let me give you simple example - easier to understand, than your collections.

It will be applied to yours at the bottom.

What you describe happens when initial Eloquent collection: $cars - has same id key as one of the following collections.

e.g. $cars has same id as element from $bikes

If you use it like this:

$vehicles = $cars->merge($bikes);

… you will get overwrite.

You get same overwrite, when you use it in this fashion as well:

$vehicles = $cars->merge($buses)->merge($bikes);

Regardless, where you put bikes when merging.

There is a simple workaround:

$vehicles = collect();
$vehicles = $vehicles->merge($buses)->merge($bikes)->merge($cars);

In this case, since your initial collection has no elements, it cannot conflict with any following collection and you get no overwrites.

It should work in any merging order, as long as empty collection comes first.

In your example, instead of:

$collectionMerge1 = $images->merge($userimages)->merge($hours);

You should do something like this:

$collectionMerge1 = collect();
$collectionMerge1 = $collectionMerge1->merge($images)->merge($userimages)->merge($hours);

Since empty collection is a base collection for merging, it does not mess with any following collection as it has no elements.

Merge two collections into one with unique array, I am new to Laravel and having some difficulty trying to find the answer to my problem. I have one DB query in a collection and another collection fro The merge method can accept either an array or a Collection instance. The merge method returns a new instance of Collection and does not modify the original collection instance. The behavior of the merge method is similar to PHP's array_merge function.

Given that each of your values actually represents a proper collection (established through relationships), you should use push instead of merge. This is because merge will actually overwrite certain elements when the key pairs already match:

$aggregates = collect([]);
$aggregates ->push($images)->push($userImages)->push($hours);
dd($aggregates ); //as expected

Collection merge, These two also deal with combining collections or arrays. The problem with merge si that if two keys are the same, only one will survive. Posted 3 years ago by� In case you prefer merge() method to combine two collections (relationships), it will override elements with the same index keys so you will loose some of your data gained from one relationship. You should choose push() method instead, which creates new array keys by pushing one collection to the end of other collection. Here is a sample :

merge collections laravel Code Example, Get code examples like "merge collections laravel" instantly right from laravel updating timestamp column error Failed to parse time string at� Laravel - Collection merge() returning a single row. Related. 435. Laravel requires the Mcrypt PHP extension. 420. How to Create Multiple Where Clause Query Using

How to merge Laravel objects in controller, Collection::merge uses PHP's array_merge, which overwrites string keys but not integer keys. If that's a problem, you can always subclass Laravel Collection to� Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit

  • $images->concat($userimages)->concat($hours); will do the job a well
  • agreed, was most of all for understanding as this question in general seems to be a lot of confusing aobut it :)
  • I tried but when you use push, you don't merge collections in a single one, instead, what you are doing is to add more collections and not merging them.