Handling Laravel Pagination

In this article, we will discuss how to handle Laravel Pagination. Laravel paginator is integrated with the query builder and Eloquent ORM. It provides an easy way to handle the pagination of database results on the query builder or an Eloquent query. Default, pagination links HTML template is compatible with Bootstrap CSS framework. It’s easy to customize the HTML generated by the Laravel paginator.

You can get more details for Laravel Routing here.

# Eloquent Result Pagination

It’s easy to apply pagination on eloquent queries. In given example, we will paginate the Post model with 10 items per page.

$posts = App\Post::paginate(10);

You can also apply other constraints on the query with paginator. For example:

$posts = App\Post::where('status','active')->paginate(10);

Simple Pagination

When you need to show simple “Next” and “Previous” links in your pagination view. Then you can use the “simplePaginate” method. It’s useful for the large record set where you don’t want to show all the paginator links.

$posts = App\Post::where('status','active')->simplePaginate(10);

Query Builder Result Pagination

Laravel query builder is an easy way to work with direct queries without an eloquent. Paginator methods are fully compatible with the query builders. So it’s easy and simple to add pagination with query builder dataset.

We can discuss more on Laravel Database Query Builder in our next tutorials. Let’s focus on the pagination. For example:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;

class PostController extends Controller
{
  public function Post()
  {
    $posts = DB::table('post')->paginate(10);

    return view('post.index', ['posts' => $posts]);
  }
}

Manually Create a Paginator

Laravel also provides the feature to create manual paginator. We can do this by creating an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance, as per our needs. We discuss this in our next article.

Display Pagination Links

No need to add other logic or code to show the pagination link in the view file. When we send the data from the controller to our view file. Then we can render the page links using links() method. For example:

<div class="container">
  <div class="row">
    <div class="col-6">
      <ul class="list-unstyled">
          @foreach ($posts as $post)
              <li>{{ $post->post_title }}</li>
          @endforeach
      </ul>
    </div>
    {{ $posts->links() }}
  </div>
</div>

The links method render the pagination links. Each of the links contains the page query string variable. The HTML created by the links method is used the bootstrap CSS framework styles.

Customize paginator URI

Laravel provides a withPath method to customize paginator URI. For example, when we want the paginator to generate links like http://domain.com/customize/url?page=x, then you should use the withPath method and pass custom/URL to create the link:

Route::get('posts', function () {
  $posts = App\Post::paginate(10);

  $posts->withPath('customize/url');
});

Customizing Pagination View

If you want to apply your custom template for the pagination links then you can pass the custom view name in the links method. For example:

{{ $paginator->links('custom-view.name') }}

As I already mention, paginator uses the Bootstrap CSS framework. So if you are not using the bootstrap in your application then you can create your own pagination template. You can send your data to your custom view for pagination. For example:

{{ $paginator->links('view.name', ['posts' => $posts]) }}

Now the question is where to create that view. Here’s, the easiest way to export the default pagination view is the given artisan command. And the view is located at resources/views/vendor directory using the vendor:publish command:

php artisan vendor:publish --tag=laravel-pagination

This command will place the views in the resources/views/vendor/pagination directory. You may edit this file to modify the pagination HTML.

If you would like to designate a different file as the default pagination view, you may use the paginator’s defaultView and defaultSimpleView methods within your AppServiceProvider:

use Illuminate\Pagination\Paginator;

public function boot()
{
    Paginator::defaultView('pagination::view');

    Paginator::defaultSimpleView('pagination::view');
}

Available Paginator Instance Methods

List of available paginator instance methods given below. Each method provides additional information.

$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)

In this article, we are discussing Laravel Pagination. We will discuss more Laravel features in future. Feel free to comment for any query.

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

LaravelLaravel PaginationLaravel PaginatorLaravel Routing
Comments (0)
Add Comment