Laravel Pagination Get Total Page Example

Laravel 8 , Laravel 7 , Laravel , Laravel 6

Nicesnippets

759

13-03-2021


Hi Guys,

In this example,I will learn you how to get total page pagination laravel.you can easy and simply Pagination current Button in laravel.

we can add current link on pagination using total() in laravel 6, laravel 7 and laravel 8 application. laravel provide new eloquent method total() for adding simple pagination with only current button link.

Create Route:


In first step, we will create simple routes for getting users and view it, so let's add new route in web.php file:

routes/web.php

Route::get('users', 'UserController@index');

Create Controller:

Now, we will create new UserController with index() method. in index() we will write simple pagination code. so let's create as like bellow:

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

$users = User::paginate(10)->total();

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

}

}

Create View File:

In this last step, we will create simple blade file and display users with pagination. so let's add bellow code:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Pagination Get Total Page Example - NiceSnippest.com</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 total Page Example - NiceSnippest.com</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>

{{ $users->links() }}

</div>

</body>

</html>

It will help you...