Append a Laravel collection with another collection

Append a Laravel collection with another collection

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Append a Laravel Collection with Another Collection

When you need to combine two Eloquent collections in Laravel, the default instinct is often to grab everything from both models and merge them into a single array or collection. A common scenario involves pulling all entries from one table and appending records from another, perhaps to create a unified feed or a paginated result set that spans multiple entities.

Consider a situation where you have an Entry model representing standard content and a Post model representing blog posts. If your goal is to display both types of content in a single list, the most straightforward approach using Laravel's collection methods looks like this:

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

$combined = $entries->merge($posts);

At first glance, $combined contains every row from both tables. However, there is a critical distinction between "appending" and "merging." The merge() method does not simply tack new items to the end of an array; it performs a union operation. This means it checks for duplicates based on the keys of the arrays involved.

In Laravel collections, these keys are generated by default as the model's primary key unless you explicitly define otherwise. If both your Entry and Post tables share a column named id, and that column contains unique integer values across both tables (for example, IDs 1 through 5 in one table and 1 through 3 in another), merge() will not create a list of six or eight items. Instead, it will retain only the item with the highest ID for any given key value.

If you run $entries->merge($posts) where both collections have an item with id = 5, the resulting collection will contain exactly one entry for that ID. The version coming from the second collection ($posts) overwrites the version from the first ($entries). This behavior is identical to how PHP array merging works when keys collide, but it often leads to silent data loss in Eloquent collections where developers expect a simple concatenation of rows.

If your objective is strictly to append the items so that they appear sequentially after the original collection without losing duplicates based on the primary key, merge() is not the right tool. Instead, you should use the concat() method. This method treats the incoming collection as a list of values to be appended to the end, ignoring existing keys entirely.

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

With concat(), even if both collections have items with id = 5, you will now have two distinct objects in your final array. The first object remains from the Entry collection, and a second object with the same ID is added to the end of the list. This ensures that no rows are eaten or overwritten during the combination process.

It is also worth noting how this interacts with Eloquent relationships. If your models have many-to-one relationships defined (e.g., both tables belong to the same user), you might consider eager loading those relationships before merging or concatenating to avoid N+1 query issues. However, once the collections are merged or concatenated, the underlying relationship data persists based on what was loaded in each individual query.

If you need to maintain a specific order where all Entry records come first followed immediately by Post records, concat() preserves that order perfectly. If you instead needed a unified list sorted by a specific column regardless of the model type, you would have to collect the models into a single array and call sortBy(), though this requires ensuring your sorting logic can handle the polymorphic nature of the data.

Ultimately, choosing between merge() and concat() depends on whether duplicate primary keys represent the same logical record or distinct records that happen to share an ID value. Understanding this distinction prevents silent data loss when building composite lists in Laravel 5.3 and later versions.

Our network