Laravel Pagination Get Next Page Example

Laravel 8

Nicesnippets

149

30-04-2021


Laravel Pagination Get Next Page Example

Hi guys,

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

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

Solution


{{ $users->nextPageUrl() }}

Step 1: 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 Next Page Example</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 Next Page Example</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>

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

Run your project in following command:

php artisan serve

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

Output: