ItSolutionStuff.com

Merge Multiple Collection Paginate in Laravel

By Hardik Savani • April 16, 2024
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, laravel 8, laravel 9, laravel 10 and laravel 11.

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:

web/routes.php

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

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

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

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

dd($data);

});

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

app/Providers/AppServiceProvider.php

<?php

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(),

$perPage,

$page,

[

'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

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Specific Attributes from Laravel Collection?

Read Now →

Laravel Collection Check Contains Value Example

Read Now →

Laravel Collection keys() Method Example

Read Now →

Laravel Collection intersect() and intersectByKeys() Method Example

Read Now →

Laravel Collection Except() Method Example

Read Now →

Laravel Collection Duplicates Method Example

Read Now →

Laravel Collection contains() and containsStrict() Methods Example

Read Now →

Laravel Collection SortBy Tutorial with Examples

Read Now →

Laravel Collection Merge | How to Merge Two Eloquent Collection?

Read Now →

Laravel Collection Unique | Remove Duplicates from Collection Laravel

Read Now →

Laravel Collection Search Method Example

Read Now →

Laravel Change Date Format using Carbon Example

Read Now →

How to Get Last Inserted Id in Laravel?

Read Now →

Laravel Create JSON File & Download From Text Example

Read Now →