In this video we are going to learn about Pagination.
When we have a huge amount of the records in a table and we want to show these records on the single page.
Its 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, it will split our records into pages,
Thats why use pagination is good practices in laravel.
So lets see how can we use pagination in laravel 8.
So for that first of all go to our database and here click on users table.
You can see in this table is empty.
So first off all lets insert thousands of records in this table.
So inserting thousands of records.
I am just going to use faker.
So goto the seeder folder inside the database folder.
Ok now open DatabseSeeder.php file and here inside the run function.
First off all import the faker so write here.

use Faker\\Factory as Faker;



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 lets 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 PaginationController and here just create a function.


public function allUers()
{
$users = User::paginate(10);
return view('users',['usres'=>$users]);
}

Now create view.
Goto the view folder inside the resource folder and here create a new file.
Lets say view name is users.blade.php.
Now goto the web.php file and here create route.

Route::get('/users','HomeController@allusers');


Now go to the users.blade.php file and 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 lets check this.
So switch to the browser and go to the url /users.
You can see, 10 records are showing here.
If click on next it will shows next ten records.
If I change the page size and lets set the page size 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.