10-Apr-2023
.
Admin
Hi guys, Today i will explained to the Laravel Pagination Set Per Page Tutorial in your laravel project.The Laravel Pagination Set Per Page is so easy to use.so you can just follow my step by step and learn Laravel Pagination Set Per Page Tutorial.
So let's start to the example and follow to the my all step.
Solution
-- controller --
User::paginate($request->get('per_page', 25))
-- blade file --
{!! Form::open([ 'url' => route('users'), 'method' => 'get' ]) !!}
{!! Form::select( 'per_page', [ '25' => '25', '50' => '50', '75' => '75', '100' => '100'], '25', array('onchange' => "submit()") ) !!}
{!! Form::close() !!}
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(Request $request)
{
$users = User::paginate($request->get('per_page', 25));
return view('users', compact('users'));
}
}
Step 2 : Install Laravel Collective
Here, we will install laravelcollective/html composer package for form builder so open your terminal and run bellow command:
composer require laravelcollective/html
Step 3: 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 Set Per Page Tutorial</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 Set Per Page Tutorial</h1>
{!! Form::open([ 'url' => route('users'), 'method' => 'get' ]) !!}
{!! Form::select( 'per_page', [ '5' => '5', '10' => '10', '15' => '15', '100' => '100'], '5', array('onchange' => "submit()") ) !!}
{!! Form::close() !!}
<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