Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Desory's avatar

How to manually do the onEachSide() function in Laravel 5.5 (this does exist in 5.7)

I need to limit the pagination to 5 posts in total, 2 left, active, 2 right, in 5.7 this is easily doable with this function onEachSide(), but in 5.5 it does not yet exist. any help is appreciated

0 likes
4 replies
Snapey's avatar

Below is a simple solution that shows 2 entries on either side (if there are pages). You will see a reduced number of links as you approach either end

<ul class="pagination">
    @php($cur = $users->currentPage())
    @php($total = $users->lastPage())

    @if($cur-2 > 0) <li class="page-item"><a class="page-link" href="{{ $users->url($cur-2) }}">{{ $cur-2 }}</a></li>@endif
    @if($cur-1 > 0) <li class="page-item"><a class="page-link" href="{{ $users->url($cur-1) }}">{{ $cur-1 }}</a></li>@endif
    
    <li class="page-item active"><span class="page-link">{{ $cur }}</span></li>

    @if($cur+1 <= $total) <li class="page-item"><a class="page-link" href="{{ $users->url($cur+1) }}">{{ $cur+1 }}</a></li>@endif
    @if($cur+2 <= $total) <li class="page-item"><a class="page-link" href="{{ $users->url($cur+2) }}">{{ $cur+2 }}</a></li>@endif
</ul>

It uses the standard pagination class to get the counters. Replace $users with your collection being paginated

(uses Bootstrap 4 for formatting)

jlrdw's avatar

This does not show second page and next to last page. And has two on each side.

https://imgur.com/zwNEDlz

Template custompager.blade.php:

@if ($paginator->hasPages())
    <ul class="pagination pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled"><span>«</span></li>
        @else
            <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
        @endif

        @if($paginator->currentPage() > 3)
            <li class="hidden-xs"><a href="{{ $paginator->url(1) }}">1</a></li>
        @endif
        @if($paginator->currentPage() > 4)
            <li><span>...</span></li>
        @endif
        @foreach(range(1, $paginator->lastPage()) as $i)
            @if($i >= $paginator->currentPage() - 2 && $i <= $paginator->currentPage() + 2)
                @if ($i == $paginator->currentPage())
                    <li class="active"><span>{{ $i }}</span></li>
                @else
                    <li><a href="{{ $paginator->url($i) }}">{{ $i }}</a></li>
                @endif
            @endif
        @endforeach
        @if($paginator->currentPage() < $paginator->lastPage() - 3)
            <li><span>...</span></li>
        @endif
        @if($paginator->currentPage() < $paginator->lastPage() - 2)
            <li class="hidden-xs"><a href="{{ $paginator->url($paginator->lastPage()) }}">{{ $paginator->lastPage() }}</a></li>
        @endif

        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">»</a></li>
        @else
            <li class="disabled"><span>»</span></li>
        @endif
    </ul>
@endif

Even though the docs explain usage of a custom template, I'll show simple usage here:

As example, say I have made a folder under views called cpag, and named the custom template custom:

views/cpag/custom.blade.php

Then in the view where used,

{{ $dogs->links('cpag.custom') }}

of course if you have appends then:

{{ $dogs->appends($append_array)->links('cpag.custom') }}

And as be seen from the following image, you can write custom templates anyway you need.

In case you can't see image: https://i.imgur.com/qQsiLNN.jpg

Reference:

https://laravel.com/docs/5.8/pagination#customizing-the-pagination-view

from

https://laravel.com/docs/5.8/pagination

Of course also read https://laravel.com/docs/5.8/pagination#customizing-the-pagination-view

patrickadvance's avatar
/**
     * Set the number of links to display on each side of current page link.
     *
     * @param  int  $count
     * @return $this
     */
    public function onEachSide($count)
    {
        $this->onEachSide = $count;

        return $this;
    }

Please or to participate in this conversation.