Laravel 8 Tutorial - Pagination
In this tutorial, we are going to learn about pagination.
When we have a huge amount of records in a table and we want to show these records on a single page, it's not possible to load all records in one shot. It will take more time to load all records in the page.
So, in this case, we use pagination, which will split our records into pages. That's why using pagination is a good practice in Laravel.
So, let's see how we can use pagination in Laravel 8. So, for that, first of all, go to our database and here, click on the users table. You can see that this table is empty.
So, first of all, let's insert thousands of records in this table. So, I am just going to use Faker. So, go to the seeder folder inside the database folder. Ok, now open the DatabaseSeeder.php file and here, inside the run function.
First of all, import the Faker, so write here:
use Faker\Factory as Faker;
And then:
public function run()
{
$faker = Faker::create();
foreach (range(1,1000) as $index) {
DB::table('users')->insert([
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt('secret'),
]);
}
}
Now, execute this seeder. For that, go to the command prompt and type here:
php artisan db:seed
Alright, now let's check the users table, and here you can see the records.
Now, create a controller. So, switch to the command prompt and run the command:
php artisan make:controller PaginationController
Now, open the PaginationController and here, just create a function:
public function allUers()
{
$users = User::paginate(10);
return view('users',['usres'=>$users]);
}
Now, create a view. Go to the view folder inside the resource folder and here, create a new file. Let's say the view name is users.blade.php.
Now, go to the web.php file and here, create a route:
Route::get('/users','HomeController@allusers');
Now, go to the users.blade.php file and write the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Pagination</title>
{{-- Add here bootstrap css cdn --}}
</head>
<body>
<section>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
All Users
</div>
<div class="card-body">
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $users->links() }}
</div>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
Now, let's check this. So, switch to the browser and go to the URL /users. You can see, 10 records are showing here. If you click on next, it will show the next ten records.
If I change the page size and let's set the page size to 20. Now, refresh the page, and here you can see 20 records in a page.
So, in this way, you can use pagination in Laravel 8.