How to properly merge multiple collections in Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Merging multiple collections into one is a common operation performed by developers dealing with data sets in Laravel applications. However, there might be cases where some or all of the collections are empty or contain invalid objects. Addressing these situations requires an efficient and elegant solution without unnecessary repetition or errors.
Let's break down the issues faced when attempting to merge multiple collections and possible solutions:
Issue 1: Invalid Objects in Collections
$collection1 = Collection::make([new TestClass()]);
$collection2 = Collection::make(['invalid']); // This is not actually an object
$allItems = $collection1->merge($collection2);
// Output: Call to merge() on non-object
In this scenario, Laravel generates the above error because it tries to run merge() on a string. To resolve this issue, we can perform the validation before merging the collections:
$allItems = [];
if ($collection1->count()) {
$allItems = $collection1->merge($collection2);
} else if ($collection2->count()) {
$allItems = Collection::make($collection2);
}
However, this approach can be repetitive and complex. To improve the readability and maintain code, we can use a more elegant solution:
Solution 1: Use the 'when' Method to Merge Collections Efficiently
$allItems = Collection::make([])
->when($collection1->count(), function() use (&$allItems) {
$allItems = $allItems->merge($collection1);
})
->when(function() use (&$allItems, $collection2) {
return !$allItems->count();
}, function() use (&$allItems, $collection2) {
$allItems = $allItems->merge($collection2);
});
This solution uses the 'when' method to check whether each collection is valid. Once a collection has been merged, it updates the $allItems instance. This results in an efficient and elegant way of merging multiple collections.
Issue 2: Handling Empty or Invalid Collections
$collection1 = Collection::make([]); // An empty collection
$collection2 = []; // A valid array/collection
$allItems = $collection1->merge($collection2);
// Output: Call to merge() on non-object or a collection with 0 objects
This issue can be addressed by checking whether the collections contain any items before merging them. Here's an example of handling these cases:
$allItems = Collection::make([]);
if ($collection1->count()) {
$allItems = $collection1->merge($allItems);
} else if ($collection2->count()) {
$allItems = $allItems->merge(optional($collection2)); // Merge only valid collections
}
Using the 'optional' method, we safely merge non-empty collections without triggering any errors. This approach allows us to efficiently handle multiple collections with varied states and ensures the merged collection is always valid and meaningful.
Conclusion
Merging multiple collections in Laravel applications involves various challenges, such as handling invalid or empty collections. With the techniques discussed above, we can create elegant solutions that ensure the merged collection remains valid. By using the 'when' method and checking collections for validity before merging them, we achieve efficient code with cleaner implementations.