Laravel 5.3 - Customizing pagination templates with example

Laravel 5.3 - Customizing pagination templates with example

In this post, i am going to tell you how to customize pagination view in Laravel 5.3.

In most of the application, pagination is very common task and Laravel PHP Framework has excellent library for pagination and with version of Laravel 5.3, you can easily customize pagination template either creating a own pagination file manually or using exist option is to run artisan command to publish the pagination template.

And this is the new feature that is added with Laravel 5.3.

As all know Laravel has default bootstrap pagination view and i here let you know :

Basic example of pagination that use default pagination view
  1. // routes file
  2. Route::get('products', function () {
  3. return view('products.index')
  4. ->with('products', Product::paginate(20));
  5. });
  1. // resource/views/products/index.blade.php
  2. @foreach ($products as $product)
  3. <!-- show the product details -->
  4. @endforeach
  5. {!! $products->links() !!}

Above example will give you simple default Laravel pagination view.

Ok, Now i will tell you how to pass custom view for Laravel pagination.

Custom Pagination Template in Laravel 5.3

If you go through publish file using php artisan command then run following command :

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

By running above command you will see the pagination directory in following path resources/views/vendor/ and in pagination directory you will get 4 files by default:

  • bootstrap-4.blade.php
  • default.blade.php
  • simple-bootstrap-4.blade.php
  • simple-default.blade.php

Laravel use default.blade.php file for default pagination view.

You can manually create your own custom pagination template and link it with pagination links method to show pagination view as per your needs.

Complete example to create a custom pagination view

First you need to create a table with some dummy data to test this code.

Step 1: Table and Model

I have created a products table(fields : name, details) in our database with some data.

Now create a model corresponding to the table:

app/Product.php
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Product extends Model
  5. {
  6.     public $fillable=['name','details'];
  7. }
Step2: Add route

In this step, simple add a new route for custom pagination in web.php

routes/web.php

Route::get('custom-pagination','ProductController@index');

Step3: Create ProductController.php

In this step create a ProductController.php with index method.

app/Http/Controllers/ProductController.php
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Product;
  5. class ProductController extends Controller
  6. {
  7.     public function index(Request $request){    
  8.      $products=Product::paginate(5);
  9.      return view('products',compact('products'))->with('i', ($request->input('page', 1) - 1) * 5);
  10.     }
  11. }
Step 4: Create products.blade.php file

In this step i have created a products.blade.php in following directory resources/views/products.blade.php

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Customizing pagination templates with example in Laravel 5.3</title>
  5.     <link rel="stylesheet" type="text/css" href="http://www.expertphp.in/css/bootstrap.css">
  6. </head>
  7. <body>
  8. <div class="container">
  9.     <div class="table-responsive">
  10.      <table class="table table-bordered">
  11.      <thead>
  12.      <tr class="heading">
  13.      <th>No</th>
  14.      <th>Name</th>
  15.      <th>Details</th>
  16.      </tr>
  17.             </thead>
  18.             <tbody>
  19.              @foreach ($products as $product)
  20.                  <tr>
  21.                  <td>{{ ++$i }}</td>
  22.                  <td>{!! $product->name !!}</td>
  23.                  <td>{!! $product->details !!}</td>
  24.                  </tr>
  25.                 @endforeach
  26.             </tbody>
  27.         </table>
  28.         {!! $products->links('pagination') !!}
  29.     </div>
  30. </div>
  31. </body>
  32. </html>

In above links method, i pass the view name pagination to identify which custom template i am going to use for pagination.

Step5: create a custom template pagination.blade.php

Now in this last step we will create a template which is used by a single paginator.

resources/views/pagination.blade.php
  1. @if ($paginator->hasPages())
  2. <ul class="pager">
  3. {{-- Previous Page Link --}}
  4. @if ($paginator->onFirstPage())
  5. <li class="disabled"><span>? Previous</span></li>
  6. @else
  7. <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">? Previous</a></li>
  8. @endif
  9. {{-- Pagination Elements --}}
  10. @foreach ($elements as $element)
  11. {{-- "Three Dots" Separator --}}
  12. @if (is_string($element))
  13. <li class="disabled"><span>{{ $element }}</span></li>
  14. @endif
  15. {{-- Array Of Links --}}
  16. @if (is_array($element))
  17. @foreach ($element as $page => $url)
  18. @if ($page == $paginator->currentPage())
  19. <li class="active my-active"><span>{{ $page }}</span></li>
  20. @else
  21. <li><a href="{{ $url }}">{{ $page }}</a></li>
  22. @endif
  23. @endforeach
  24. @endif
  25. @endforeach
  26. {{-- Next Page Link --}}
  27. @if ($paginator->hasMorePages())
  28. <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">Next ?</a></li>
  29. @else
  30. <li class="disabled"><span>Next ?</span></li>
  31. @endif
  32. </ul>
  33. @endif

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort