How to Create Pagination in Laravel 8

How to create pagination in laravel


In this post, you will learn how to make / create pagination in laravel 8.

The paginate method counts the total number of records matched by the query before retrieving the records from the database. This is done so that the paginator knows how many pages of records there are in total.

So guys now, you have already setup with your laravel application and now you want to add pagination in your page for your required Model. eg: Student Model. 

Step 1: Create a route in routes/web.php file and paste below code:

<?php
use Illuminate\Support\Facades\Route;

Route::get('students', [App\Http\Controllers\StudentController::class, 'index']);


Step 2: go to your controller, so I am using StudentController.php and use the paginate() while calling your Model as given below:

<?php

namespace App\Http\Controllers;

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

class StudentController extends Controller
{
    public function index()
    {
        $students = Student::paginate(10);
        return view('student.index', compact('students'));
    }
}


Step 3: Create a blade file index.blade.php in student folder OR go to the following path in your application and call the links() as given below: 

{{-- Example --}}
<div class="float-end">
    {{ $students->links() }}
</div>
@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h4>How to create Pagination in Laravel 8</h4>
                </div>
                <div class="card-body">

                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Course</th>
                                <th>Section</th>
                                <th>Edit</th>
                                <th>Delete</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($students as $item)
                            <tr>
                                <td>{{ $item->id }}</td>
                                <td>{{ $item->name }}</td>
                                <td>{{ $item->email }}</td>
                                <td>{{ $item->course }}</td>
                                <td>{{ $item->section }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

                    <div class="float-end">
                        {{ $students->links() }}
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>


Thank you.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT