Laravel PHP Framework Pagination Example
Laravel 12-Aug-2020

Laravel PHP Framework Pagination Example

In this article, We’ll show you how to pagination in laravel, this example also work with laravel version 5.8, 5.7 & 5.6 with example step by step. If you are learner this article very help full for you. Read the full article and learn how to use the pagination in laravel. And this example also work with laravel 5.8 version.

Contents

  • Install Laravel
  • Configuration .evn file
  • Run Migration
  • Add Fake Data
  • Create Route, Controller & Blade View
  • Start Development Server
  • Conclusion

1. Install Laravel

First We need Download fresh Laravel setup. Use the below command to download the laravel fresh setup on your system.

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

2. Configuration .env file

In this step, we will set database credential in .env file

laravel datatables

configuration.evn

3. Run Migration

We need to do  migration of tables using below command:

 php artisan migrate 

This command will create tables in our database.

4. Add Fake Records

We need to add some records in database. Use the below command for add fake records in your database.

php artisan tinker

After run the php artisan tinker. Use the below command. This command will add 150 fake records in your database factory(App\User::class, 150)->create();

>>> factory(App\User::class, 150)->create();

5. Create Route, Controller & Blade View


Add Route

Now we will add routes in web.php file as like below.

Open routes/web.php file

Route::get('users', 'UsersController@index');

Create Controller

We need to create new controller UsersController that will manage two method. lets use this below command and create Controller.

php artisan make:controller UsersController

Now open the controller let’s go to the => app/Http/Controllers/UsersController.php. Put the below Code

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Redirect,Response,DB,Config;
use App\User;
 
class UsersController extends Controller
{
    public function index()
    {   
        $users = User::paginate(10);
        return view('users',$users);
    }
   
}

Create Blade View

Next, create users.blade.php file in resources/views/ folder and copy past following code.

<!DOCTYPE html>
 
<html lang="en">
<head>
<title>Laravel Pagination - Tuts Make</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">  
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>  
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
      <body>
         <div class="container">
            <h2>Laravel Pagination - Tuts Make</h2>
            <table class="table table-bordered" id="laravel">
               <thead>
                  <tr>
                     <th>Id</th>
                     <th>Name</th>
                     <th>Email</th>
                     <th>Created at</th>
                  </tr>
               </thead>
               <tbody>
                  @foreach($users as $user)
                  <tr>
                     <td>{{ $user->id }}</td>
                     <td>{{ $user->name }}</td>
                     <td>{{ $user->email }}</td>
                     <td>{{ date('d m Y', strtotime($user->created_at)) }}</td>
                  </tr>
                  @endforeach
               </tbody>
            </table>
         {!! $users->links() !!}
         </div>
   </body>
</html>

 

6. Start Development Server

In this step, we will use the php artisan serve command . It will start your server locally

php artisan serve

Run the project diffrent port so use this below command 

php artisan serve --port=8080  

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/users

If you are not run php artisan server command , direct go to your browser and type the url

http://localhost/firstApp/public/

7. Conclusion

In this article , We have successfully use simple laravel pagination with example . our examples run quickly.

Laravel Pagination Instance Methods

Use some of the laravel pagination method in your application. Each paginator instance provides additional pagination information via the following methods.

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