Using multiple pagination types in the same application

Submitted by coucou - 10 years ago

Sometimes, you may want to use different pagination types across your application. By default, Laravel will use the type specified in your app/config/view.php file, so you need to override this setting when you wish to use another type. Here is how to do so.

// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.

$articles = Article::paginate(5);

Paginator::setViewName('pagination::simple');

/*
Alternatively, you could also use this to achieve the same result:

$articles->getEnvironment()->setViewName('pagination::simple');

For those who would like to know what’s happening under the hood, here is a more
detailed explanation:

1. Calling paginate() on an Eloquent model or a query builder will return an
   instance of \Illuminate\Pagination\Paginator
   
2. Then, we need to get the related \Illuminate\Pagination\Environment of this
   paginator via the well-named getEnvironment() method.
   
3. Finally, we can specify the pagination type we need. The default value is
   'pagination::slider'.
   
The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/