Merge Multiple Collection Paginate in Laravel Example

Merge Multiple Collection Paginate in Laravel Example

In this post we will give you information about Merge Multiple Collection Paginate in Laravel Example. Hear we will give you detail about Merge Multiple Collection Paginate in Laravel ExampleAnd how to use it also give you demo for it if it is necessary.

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.

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 = AppUser::get();

$product = AppProduct::get();

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

dd($data);

});

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

app/Providers/AppServiceProvider.php

Also see:Task Scheduling with Cron Job in Laravel 5.8

<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;

use IlluminateSupportCollection;

use IlluminatePaginationLengthAwarePaginator;

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…

Hope this code and post will helped you for implement Merge Multiple Collection Paginate in Laravel Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

21  +    =  24

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US