Merge Multiple Collection Paginate in Laravel Example

By Hardik Savani | May 3, 2019 | Category : Laravel

If you are merge multiple laravel collection and you need to paginate with laravel collection then i will show you how to paginate a merged collection in laravel 5.8 application. you can easily make pagination of multiple merge collection in laravel 5, laravel 6, laravel 7 and laravel 8.

We will create 'paginate' function using Collection macro method. You need to register that method to AppServiceProvider. i will give you very simple example to understand how it is working for laravel paginate collection or array.

So, basically you can make manually pagination with laravel collection. let's see bellow example:


Route::get('collection-array', function(){

$user = \App\User::get();

$product = \App\Product::get();

$data = $user->merge($product)->paginate(10);



Now you need to register collection paginate method on AppServiceProvider file.



namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Collection;

use Illuminate\Pagination\LengthAwarePaginator;

class AppServiceProvider extends ServiceProvider



* Register any application services.


* @return void


public function register()



* Paginate a standard Laravel Collection.


* @param int $perPage

* @param int $total

* @param int $page

* @param string $pageName

* @return array


Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {

$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

return new LengthAwarePaginator(

$this->forPage($page, $perPage),

$total ?: $this->count(),




'path' => LengthAwarePaginator::resolveCurrentPath(),

'pageName' => $pageName,






* Bootstrap any application services.


* @return void


public function boot()




Now you can check it...

I hope it can work...

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: