How can I paginate a collection?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Paginate Merged Collections in Laravel Applications Body:

When working on applications that deal with large amounts of data, pagination becomes essential to keep the user experience smooth and efficient. While Laravel provides a built-in way to paginate eloquent models and database queries, it doesn't directly support collections. This issue arises when you need to paginate results resulting from merging multiple collections.

Understanding Collections and Merging

In Laravel, collections can be created by retrieving data from your database or external sources like APIs. They provide a convenient way to manipulate the contents of this data without worrying about indexes or relationships between them. In some cases, however, you may need to merge multiple collections together for display purposes or further processing.

Why Can't We Paginate Collections Directly?

The reason why Laravel doesn't natively support paginating collections is because collections are essentially a collection of data without an underlying database or model. They are not associated with any table in your database like models are – they are just arrays containing data. Pagination, on the other hand, requires knowledge about how many records there are to paginate and how to split them into groups. Since this information is not present in collections, you need to handle it by yourself.

Manually Creating a Pager

While Laravel doesn't provide direct support for paginating merged collections, it's still possible to create your own custom paging system. You can use the built-in pagination capabilities of Laravel and combine them with your custom paginator to get the desired effect. Here is a basic example:

public function index()
{
    $bluePerson = BluePerson::all();
    $redPerson = RedPerson::all();

    $people = $bluePerson->merge($redPerson)->sortByDesc('created_at');
    // Get total count of merged people
    $totalCount = count($people);

    // Create a Paginator instance with the required number of items per page (e.g., 20) and the total count retrieved from merged collections
    $paginator = new \Illuminate\Pagination\LengthAwarePaginator(collect([]), $totalCount, 20);

    return view('stream.index')->with('people', $people)->with('pager', $paginator);
}

Conclusion

In summary, while direct pagination for collections is not available in Laravel, you can still create your own custom paging system by leveraging the built-in pagination capabilities and combining them with a manual implementation. By following this approach, you will be able to efficiently manage your merged collections without losing functionality.

Note: It's essential to keep in mind that creating paginators manually introduces some complexity and potential security issues if not handled correctly. Always consider using the official Laravel tools when possible and follow best practices for handling data retrieval and manipulation.