Zarunet
Zarunet

Reputation: 38

Laravel show all links in paginator

Since Laravel 5.7, the paginator links() method shows only a reduced number of pages when called. The number of links can be customized using the onEachSide($count) method.

‹‹ ‹ 1 2 ... 47 48 49 50 51 52 53 ... 102 103 › ››

I want to display every page in order to make a page selector dropdown, using a html <select>.

Calling onEachSide(9999) does work, but it looks flimsy, and may break on high page counts that the app i'm writing could reach.

What would be the clean way to get all the page links ?

EDIT: I am using a custom view, which looks like the following :

@if ($paginator->hasPages())
    <ul class="pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled">&lsaquo;&lsaquo;</li>
            <li class="disabled">&lsaquo;</li>
        @else
            <li><a href="{{ $paginator->toArray()['first_page_url'] }}">&lsaquo;&lsaquo;</a></li>
            <li><a href="{{ $paginator->previousPageUrl() }}">&lsaquo;</a></li>
        @endif

        {{--Pagination Elements --}}
        @foreach ($elements as $element)
            @if (is_string($element))
                <li class="disabled"><span>{{ $element }}</span></li>
            @endif

            @if (is_array($element))
                @foreach ($element as $page => $url)
                    @if ($page == $paginator->currentPage())
                        <li class="active"><span>{{ $page }}</span></li>
                    @else
                        <li><a href="{{ $url }}">{{ $page }}</a></li>
                    @endif
                @endforeach
            @endif
        @endforeach

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

The $elements variable seems to already have the extra pages removed, however.

Upvotes: 1

Views: 1672

Answers (1)

Jerodev
Jerodev

Reputation: 33186

The paginator uses the bootstrap-4 blade view to render the page. This view always implements the tripple dots.
You could create a custom view that renders all pagination pages without any tripple dot items and pass this view as the first parameter to the links function.


Update: The view you are using is exactly the same as the one build in to Laravel. You shouldn't use the $element variable as this gets windowed with tripple dot elements.

You should use the pagination lastpage variable and loop over all pages yourself. For example, like this:

{{-- Pagination Elements --}}
@for ($page = 1; $page <= $paginator->lastPage(); $page++)
    @if ($page == $paginator->currentPage())
        <li class="active"><span>{{ $page }}</span></li>
    @else
        <li><a href="{{ $paginator->url($page) }}">{{ $page }}</a></li>
    @endif
@endforeach

Upvotes: 2

Related Questions