Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Custom Pagination in Laravel

Custom Pagination in Laravel

In this tutorial, I will give you an example of laravel custom pagination, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application, Sometimes we need to use customize paginate data in controller or Blade file according to project or client requirement in laravel application.

In my previous article, I already described simple pagination in laravel application, In this article, we will customize pagination.

Recommended Article : How to use pagination in Laravel 8.

Database Table :

We have a pagination_example table, where we have Sufficient records to paginate these records in the blade file.

Custom Pagination in Laravel
Custom Pagination in Laravel

web.php

Route::get('/list-data','ProductController@ListPaginationData')->name('list.data');

Controller

public function ListPaginationData(){
   $paginateData = PaginationExample::paginate(10);
   return view('paginationexample',compact('paginateData'));
 }

Blade File

 <div class="container">
         <h3>Example of Customise Pagination in Laravel.</h3>
         <br>
         <table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Book Name</th>
                  <th>Book Author</th>
               </tr>
            </thead>
            <tbody>
               @foreach($paginateData as $key => $data)
               <tr>
                  <td>{{ $key+1 }}</td>
                  <td>{{$data->book_name}}</td>
                  <td>{{$data->book_author}}</td>
               </tr>
            </tbody>
            @endforeach
         </table>
          <div class="mt-5" style="float:right">
                 
             {{ $paginateData->links()}}

             {{ $paginateData->firstItem() }} - {{ $paginateData->lastItem() }} of total {{ $paginateData->total() }} entries. 
         </div>
      </div>

Finally, we paginated the App\PaginationExample model and indicate that we plan to display 10 records per page with the total number of records. As you can see below the given images.

Custom Pagination in Laravel
Custom Pagination in Laravel
Custom Pagination in Laravel

Note :

At first, we count the total numbers of records To Get All Item Total in pagination with the help of total().

{{ $paginateData->total() }} //show total records in your Table.
(Not available when using simplePaginate).

Then we use firstItem() and lastItem() in pagination and subtract those to get the current number of records on the current page.

{{ $paginateData->firstItem() }} - {{ $paginateData->lastItem() }} of total {{ $paginateData->total() }}

In this article, we learned “How to use custom pagination in Laravel”, I hope this article will help you with your Laravel application Project.

Read Also : Add a new column in the table using migration in Laravel.