Create a custom pagination view in Laravel. We know that Laravel has by default gives us a pagination view using the bootstrap design like next and previous button and also the number page with a link. But it is very popular and is generally used by most developers. If we want to change it using a class submitted by Laravel. We can easily set our own custom width for the pagination layout.


app/Pagination/HDPresenter.php

<?php

namespace App\Pagination;

use Illuminate\Pagination\BootstrapThreePresenter;

class HDPresenter extends BootstrapThreePresenter {

    public function render() {
        if ($this->hasPages()) {
            return sprintf(
                    '<div class="pagi-custom"><div class="pull-left">%s %s</div> <div class="pull-right">%s %s</div></div>', $this->getFirst(), $this->getButtonPre(), $this->getButtonNext(), $this->getLast()
            );
        }
        return "";
    }

    public function getLast() {
        $url = $this->paginator->url($this->paginator->lastPage());
        $btnStatus = '';


        if ($this->paginator->lastPage() == $this->paginator->currentPage()) {
            $btnStatus = 'disabled';
        }
        return $btn = "<a href='" . $url . "'><button class='btn btn-success " . $btnStatus . "'>Last <i class='glyphicon glyphicon-chevron-right'></i></button></a>";
    }

    public function getFirst() {
        $url = $this->paginator->url(1);
        $btnStatus = '';


        if (1 == $this->paginator->currentPage()) {
            $btnStatus = 'disabled';
        }
        return $btn = "<a href='" . $url . "'><button class='btn btn-success " . $btnStatus . "'><i class='glyphicon glyphicon-chevron-left'></i> First</button></a>";
    }

    public function getButtonPre() {
        $url = $this->paginator->previousPageUrl();
        $btnStatus = '';


        if (empty($url)) {
            $btnStatus = 'disabled';
        }
        return $btn = "<a href='" . $url . "'><button class='btn btn-success " . $btnStatus . "'><i class='glyphicon glyphicon-chevron-left pagi-margin'></i><i class='glyphicon glyphicon-chevron-left'></i> Previous </button></a>";
    }

    public function getButtonNext() {
        $url = $this->paginator->nextPageUrl();
        $btnStatus = '';


        if (empty($url)) {
            $btnStatus = 'disabled';
        }
        return $btn = "<a href='" . $url . "'><button class='btn btn-success " . $btnStatus . "'>Next <i class='glyphicon glyphicon-chevron-right pagi-margin'></i><i class='glyphicon glyphicon-chevron-right'></i></button></a>";
    }

}


So now, here we know how to use this class in the laravel blade file, so let’s see the following file on how to extend this class on the pagination class function.


In Blade file

@extends('layouts.app')

@section('content')
<div class="container">
    <table class="table table-bordered">
        <tr>
            <th>Title</th>
            <th>Description</th>
        </tr>
        @foreach ($posts as $post)
        <tr>
            <td>{{ $post->title }}</td>
            <td>{{ $post->description }}</td>
        </tr>
        @endforeach
    </table>
    {!! with(new App\Pagination\HDPresenter($posts))->render(); !!}
</div>
@endsection

Thanks...........