Laravel Pagination Set Per Page Example

Jun 28, 2021 . Admin

Hi Guys,

Today, In this tutorial i will explain you how to create set per page tutorial in laravel you can easily use set per page in laravel pagination in laravel 8 application.

Here, I will give you full example of set per page tutorial in laravel so follow my steps.

Solution
 
User::paginate($request->get('per_page', 25))


{!! 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() !!}   
Create Route Path : routes/web.php
<?php
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('set-per-page', [HomeController::class, 'index'])->name('users');
Create Controller:

Next, In this step we will create a simple home controller just following command through.

Path : app/Http/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class HomeController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = User::paginate($request->get('per_page', 25));
        return view('users', compact('users'));
    }
}

Create Blade File

After Successfully create controller we can create a simpele blade file users.blade.php

Path : resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel Pagination Set Per Page Example - MyWebTuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body class="bg-dark"> 
   
<div class="container mt-5">
    <div class="row">
        <div class="col-md-8 offset-2">
            <div class="card">
                <div class="card-header">
                   <h5>Laravel Pagination Set Per Page Example - MyWebTuts.com</h5>
                   {!! 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() !!}
                </div>
                <div class="card-body">
                    <table class="table table-bordered table-hover">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($users as $user)
                            <tr>
                                <td>{{ $user->id }}</td>
                                <td>{{ $user->name }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </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>
            </div>
        </div>
    </div>
</div>

</body>
</html>

Output

It will help you...

#Laravel 8 #Laravel