Laravel Pagination Get Per Page Tutorial


Share:
Published By: Admin, Published On: Jun 29, 2021, Category: Laravel 8 Laravel

Hi Friends,

Today, In this article i would like to share you how to create get per page pagination example in laravel you can easily utilize get per page in laravel pagination in laravel 8 application.

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

Solution
Count : {{ $users->perPage() }} 
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('get-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(Request $request)
    {
        $users = User::paginate(5);
        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>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Laravel Pagination Get 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 Get Per Page Example - MyWebTuts.com</h5>
                        <h5 class="text-danger">Count : {{ $users->perPage() }}</h5>
                    </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->previousPageUrl() }}">Previous</a></li>
                            <li class="page-item"><a class="page-link" href="{{ $users->nextPageUrl() }}">Next</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>  
    </div>
</body>
</html>

Output

It will help you...

Share: