Simplify Your Laravel Experience: Using Pagination

Laravel Pagination | How to use Pagination in Laravel


Laravel Pagination | How to use Pagination in Laravel


               Pagination in laravel is very much easy as compared to other frameworks beacause laravel directly provides the paginate() method . You don't need to worry about how pagination will work you just need to provide the paginate() method and call it wherever you need to paginate , Laravel will automatically manage it . Paginate method in laravel is also compatible will Bootstrap and CSS Frameworks . When thousands of data you want to show to the user your cannot show it in a single page , on that case you need pagination . So let's see how laravel pagination works .

How to Use Pagination in Laravel | Laravel Pagination


               There are many ways to use pagination in laravel to access data items from database . The most easy and common way to use paginate() is on Query builder or an Eloquent query .

            As i have already told you , Laravel automatically takes care of setting up paginate() method . Whenever you are providing parameter to the paginate() method like paginate(5) ,laravel automatically understands that it has to show 5 rows or 5 data on that perticular page and paginate it and the remaining will be shown on the next page and so on .

           Don't use your own approach / logic for pagination , you might make several mistakes , it's recommended to use laravel helper method for laravel pagination because it is very much clean and simple.Always try to use the provided official methods for development .

Let's see practically How pagination in Laravel works ..

Step1 : Install Laravel 7

Use the Following command to install Laravel in your system..


composer create-project --prefer-dist laravel/laravel project-name

After project created we need to create a database and table and connect it to our project..

For this example i am creating my database named productmgmt and a table named product  and inserted some sample data into the table as you can see in the following picture .

Laravel Pagination | How to use Pagination in Laravel


Step2: Link database to your Project 

             Let's connect our database and table to our project .

  • Connecting database to project
               To connect database with your project just go inside your project and inside .env  file and make the following configuration .


 DB_CONNECTION=mysql  
 DB_HOST=127.0.0.1  
 DB_PORT=3306  
 DB_DATABASE=productmgmt   
 DB_USERNAME=root  
 DB_PASSWORD=  

      After this your database will be linked to your project...

  • Connecting database table to project
                   To connect your table with your project we need to first create a model .To create a model use the following command .


  php artisan make:model model_name  

 After successfull creation of model make the following configuration in your model file which is present inside project/App/model_name.php 


App/product.php


 <?php  
 namespace App;  
 use Illuminate\Database\Eloquent\Model;  
 class Product extends Model  
 {  
 protected $table='product';  
 public $timestamps=false;  
 }  

      Congrat's we have successfully linked our database and table in our project..

Step3: Display Data in The Frontend with Pagination

             Let's go inside our controller named createController.php and make an index() method through which we will show our data .


createController.php

 public function index()  
 {  
 $product=DB::table('product')->paginate(8);  
 return view('welcome',['product'=>$product]);  
 }  


  After paginating the result we need to add following codes inside the fronted page / where you want to show pagination

 {{ $product->links() }}  

web.php ( route )

 Route::get('/','createController@index')->name('home');  
welcome.blade.php

 <!DOCTYPE html>  
 <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">  
   <head>  
    <style>  
      img:hover  
      {  
      filter: drop-shadow(0 0 0.75rem #7C95CE);  
      transition: 0.5s all;  
      }  
      img  
      {  
      filter: brightness(120%);  
      }  
      body  
      {  
      background-image: url('resources/views/layout/bg.jpg');  
      height: 160vh;  
      width: auto;  
      background-position: center;  
      background-repeat: no-repeat;  
      background-size: cover;  
      }  
      .card  
      {  
      background: transparent!important;  
      color: #fff;  
      font-weight: bold;  
      }  
    </style>  
   </head>  
   <body>  
    @extends('layout.main')  
    @section('content')  
    @if(session('succmsg'))  
    <div class="alert alert-success alert-dismissible fade show text-center my-3" role="alert">  
      <strong>Movie Addedd Successfully</strong>  
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">  
      <span aria-hidden="true">&times;</span>  
      </button>  
    </div>  
    @else  
    @if($errors->any())  
    @foreach($errors->all() as $error)  
    <div class="alert alert-danger" role="alert">  
      {{$error}}  
    </div>  
    @endforeach  
    @endif  
    @endif  
    <div class="row mt-5">  
    {{--       
    <div class="col-2"></div>  
    --}}      
    <div class="col-12">  
      <div class="row">  
       <div class="col-1"></div>  
       <div class="col-10">  
         <div class="row">  
          @foreach($product as $data)  
          <div class="col-3">  
            <div class="card p-2" style="width: 18rem;border:none;">  
             <a href="{{route('view',$data->id)}}"><img src="{{asset('public/upload/userimg/'.$data->image)}}" style="height: 350px;border-radius: 10px;" class="card-img-top" alt="..."></a>  
             <div class="card-body">  
               <p class="card-text text-center">{{ $data->name }}</p>  
               <div class="row">  
                <div class="col-6 text-center">  
                  <a href="{{route('edit',$data->id)}}"> <button class="btn btn-warning">Edit &nbsp<i class="fa fa-pencil" aria-hidden="true"></i>  
                  </button></a>  
                </div>  
                <div class="col-6 text-center">  
                  <a href="{{route('delete',$data->id)}}"><button class="btn btn-danger">Delete &nbsp<i class="fa fa-trash" aria-hidden="true"></i>  
                  </button></a>  
                </div>  
               </div>  
             </div>  
            </div>  
          </div>  
          @endforeach  
         </div>  
       </div>  
       <div class="col-1">  
       </div>  
      </div>  
      <div class="row">  
       <div class="col-4"></div>  
       <div class="col-4 text-center">  
         {{ $product->links() }}  
       </div>  
       <div class="col-4"></div>  
      </div>  
      {{--       
      <div class="col-2"></div>  
      --}}    
    </div>  
    @endsection  
   </body>  
 </html>  



            Now your pagination is ready ..


Laravel Pagination | How to use Pagination in Laravel

Laravel Pagination | How to use Pagination in Laravel


conclusion :

                   However in other frameworks pagination become more complex but laravel makes it easier .search data with pagination in laravel is very much easy than other frameworks .In pagination just keep in mind that during the call of paginate() method in your controller user the render() method of laravel to get the paginated data .pagination in laravel 5 and pagination in laravel 7 is almost the same .

Thank you for reading this article ...

Read Also :-







Previous Post Next Post

Contact Form