In this tutorial you will learn about the Laravel 7/6 Pagination Tutorial with Example and its application with practical example.
In this Laravel 7/6 Pagination Tutorial with Example, I will show you how to create and use pagination in laravel application. In this tutorial you will learn to implement and use laravel in-built pagination. In this article we will demonstrate you with example how to use laravel inbuilt pagination methods with bootstrap tables. This tutorial will demonstrate you how to use laravel pagination with tables and display data with pagination.
Laravel 7/6 Pagination Tutorial with Example
In this step by step tutorial we will learn about laravel inbuilt pagination:
- Install Fresh Laravel
- Setup Database Credentials
- Run Migration
- Add Fake Data
- Create Route, Controller & Blade View
- Start Development Server
1. Install Fresh Laravel
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
2. Setup Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
3. Run Migration
Now, run following command to migrate database schema.
1 |
php artisan migrate |
4. Add Fake Records
In this step we will add some dummy records in database using following laravel command:
1 |
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();
1 |
>>> factory(App\User::class, 150)->create(); |
Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 |
Route::get('users', 'UsersController@index'); |
Create Controller By Artisan Command
Now, lets create a controller named UsersController using command given below –
1 |
php artisan make:controller UsersController |
Now open the controller let’s go to the => app/Http/Controllers/UsersController.php. Put the below Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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 Views
Next, create users.blade.php file in resources/views/ folder and copy past following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<!DOCTYPE html> <html lang="en"> <head> <title>Laravel Pagination</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></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 Local Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/users |