Laravel Pagination Get Per Page Tutorial

Jun 29, 2021 . Admin

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.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Route 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');
Step 3: Create Controller:

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

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'));
    }
}

Step 4: Create Blade File

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

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>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/get-per-pag
Output :

It will help you...

#Laravel 8 #Laravel