Laravel Pagination Get Per Page Example

10-Apr-2023

.

Admin

Laravel Pagination Get Per Page Example

Laravel Pagination Get Per Page Example

Hi guys,Today i will explained to the the Laravel Pagination Get Per Page Example in your laravel project.the laravel pagination is so easy to use.so you can just follow my step by step and learn Laravel Pagination Get Per Page Example.

So let's start to the example and follow to the my all step.

Solution


Count : {{ $users->perPage() }}

Step 1: Create a User Controller

Next you can require to the User Controller so create a User Controller in just following command through.

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

$users = User::paginate(10);

return view('users', compact('users'));

}

}

Step 2: Add Blade File

Last step to create a users.blade.php file and use this code.

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>laravel pagination get previous page</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<h1>laravel pagination get previous page</h1>

<h4>Count : {{ $users->perPage() }}</h4>

<table class="table table-bordered">

<tr>

<th>ID</th>

<th>Name</th>

</tr>

@foreach($users as $user)

<tr>

<td>{{ $user->id }}</td>

<td>{{ $user->name }}</td>

</tr>

@endforeach

</table>

<ul class="pagination">

<li class="page-item"><a class="page-link" href="{{ $users->nextPageUrl() }}">Next</a></li>

<li class="page-item"><a class="page-link" href="{{ $users->previousPageUrl() }}">Previous</a></li>

</ul>

</div>

</body>

</html>

Ok, now you have to create some dummy records on users table.

So, finally we are done with our code we can get below output.

php artisan serve

Browser url run : http://localhost:8000/users

Output:

#Laravel 8

#Laravel 7