Limit the pagination link amount

Published 10 months ago by dohisev

I'm using Bootstrap 4 and it seems that the pagination isn't responsive, but even so the amount of links on the current Laravel pagination is way bigger than expected and I was wondering how to make it smaller. (I'm using Laravel 5.5)

How to make it like:

[<][1][2][...][6][7][>] (example)?

tisuchi

@dohisev

May be simply you can use next or previous button by using simplePaginate() instead of paginate() function.

However, if you really need to keep the same format but reduce the numbers, in this case, I think you have only one way to do that is called customized the pagination function. Check this discussion how to do that- https://stackoverflow.com/questions/27655992/limit-amount-of-links-shown-with-laravel-pagination

jlrdw
jlrdw
10 months ago (245,090 XP)

Just write a custom presenter like:

@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

As an example. You can make the links any way you desire.

(& raquo ;) and (& laquo ;) no space did not display correctly in code block, sorry.

Please sign in or create an account to participate in this conversation.