mahbubrn's avatar

In pagination laravel not showing number

I'm stuck with a problem in my laravel 9 web app. Pagination number not showing in the blade file. Only display the "Pagination.Previous" "Pagination.Next" link.

My code is In Controller:

$list = District::active()->orderBy('name')->paginate(10);

return view('district.list', compact('list'));

In view:

@if ($list->hasPages()) {{ $list->links() }} @endif

So here what I did wrong?

0 likes
9 replies
jlrdw's avatar

Just use {{ $list->links() }}

mahbubrn's avatar

@jlrdw already I used this and tried. but pagination numbrer not showing. Only prev and next link is displying.

jlrdw's avatar

@mahbubrn Do a dd and see how many results you get. And did you apply style to paginator?

1 like
jlrdw's avatar

@mahbubrn Just needed to know how many results? 10 or less? More than 10?

Try this one:

@if ($paginator->hasPages())
    <ul class="pagination pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled"><span>&laquo;</span></li>
        @else
            <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">&laquo;</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">&raquo;</a></li>
        @else
            <li class="disabled"><span>&raquo;</span></li>
        @endif
    </ul>
@endif

Call it cpage.blade.php put directly in views folder and then:

{{ $list->links('cpage') }}

Edit:

If it works, go back and figure out how you are not calling the correct styles.

1 like
mahbubrn's avatar

@jlrdw I already tried this command

php artisan vendor:publish --tag=laravel-pagination

And the above same code tried. But not working.

jlrdw's avatar

@mahbubrn Show controller, view, model code, and first make sure you are calling the correct view, otherwise if you have more than 10 results it would work.

mahbubrn's avatar
mahbubrn
OP
Best Answer
Level 1

@jlrdw Its worked.

php artisan vendor:publish --tag=laravel-pagination

    {{ $list->links('vendor.pagination.default') }}

After fixing the correct view path. So why do I have to call a separate view file here? Why did the default system not work?

1 like
jlrdw's avatar

@mahbubrn Don't know, but just double check your code see if you missed something somewhere. Glad you got it working.

2 likes

Please or to participate in this conversation.