Laravel Pagination Get Previous Page Tutorial

Laravel 8

Nicesnippets

215

30-04-2021


Laravel Pagination Get Previous Page Tutorial

Hi guys,

Today i will explained to the the laravel pagination get previous page 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 previous page.

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

Solution


{{ $users->previousPageUrl() }}

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>

<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: