mariavilaro's avatar

Laravel 5.5 pagination links, how to customize number of elements before ... (three dots)

I know this has been asked thousands of times, but I can only find answers for Laravel versions pre 5.4 and I was wondering if something changed lately.

I found this beautiful piece of code in Illuminate\Pagination\LengthAwarePaginator, that builds the elements to show in the pagination links:

/**
 * Get the array of elements to pass to the view.
 *
 * @return array
 */
protected function elements()
{
    $window = UrlWindow::make($this);

    return array_filter([
        $window['first'],
        is_array($window['slider']) ? '...' : null,
        $window['slider'],
        is_array($window['last']) ? '...' : null,
        $window['last'],
    ]);
}

And this is UrlWindow::make function, that accepts a parameter $onEachSide for just what I need:

/**
 * Create a new URL window instance.
 *
 * @param  \Illuminate\Contracts\Pagination\LengthAwarePaginator  $paginator
 * @param  int  $onEachSide
 * @return array
 */
public static function make(PaginatorContract $paginator, $onEachSide = 3)
{
    return (new static($paginator))->get($onEachSide);
}

Sooo if the function in LengthAwarePaginator is changed just this little tiny bit, I can change the number of elements on each side of the current page before the ... appear:

protected function elements()
{
    $window = UrlWindow::make($this, 2);
    ...

Now the question is: is there any way in the world we can do this by overriding the paginator method?

It would be so beautiful if we could put this value in a Model property like we already do with $perPage... isn't it?

Any elegant solution out there?

0 likes
2 replies
jlrdw's avatar

I use this:

@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

See https://laravel.com/docs/5.6/pagination#customizing-the-pagination-view for more detail

Do not modify vendor files

2 likes
jlrdw's avatar

you have to modify the template yourself.

Please or to participate in this conversation.