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
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.


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

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Laravel Pagination Get Per Page Example -</title>
    <link rel="stylesheet" href="">
<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 -</h5>
                        <h5 class="text-danger">Count : {{ $users->perPage() }}</h5>
                    <div class="card-body">
                        <table class="table table-bordered table-hover">
                                @foreach($users as $user)
                                    <td>{{ $user->id }}</td>
                                    <td>{{ $user->name }}</td>
                        <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>

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:

Output :

It will help you...

#Laravel 8 #Laravel