DianaAli's avatar

How to get total numbers in laravel pagination?

I'd like to show like this in laravel pagination.

Showing from to to of total entries

Here is from, to , total are not page number. They are items number. Please reply me if you know.

0 likes
10 replies
tykus's avatar

https://laravel.com/api/8.x/Illuminate/Contracts/Pagination/LengthAwarePaginator.html

All of the methods on the Paginator instance are listed here.

You can see how those methods are used in the pagination views, e.g.

<p class="text-sm text-gray-700 leading-5">
	{!! __('Showing') !!}
	<span class="font-medium">{{ $paginator->firstItem() }}</span>
	{!! __('to') !!}
	<span class="font-medium">{{ $paginator->lastItem() }}</span>
	{!! __('of') !!}
	<span class="font-medium">{{ $paginator->total() }}</span>
	{!! __('results') !!}
</p>
DianaAli's avatar

I need exactly total record count. total() method gives only total page numbers.

tykus's avatar

total() method gives only total page numbers

Are you sure about that?

DianaAli's avatar

Yes, I've tried. But it gave me total page numbers not total record numbers.

jlrdw's avatar

You dont use total(), use $paginator->total()

See my last example below in last reply.

Here I'll just do it:

{{ "Showing from " .  $paginator->firstItem() . " to " . $paginator->lastItem() . " of " . $paginator->total() }}
DianaAli's avatar

Thanks for your replying me. There is no how to get total numbers. I just used total() method but it gives only total page numbers not item numbers.

jlrdw's avatar

It's in example:

$paginator->lastPage()   //  is total pages.

Okay you want record count. Get count in query and replace above line with your count, just pass it in.

$paginator->total()
DianaAli's avatar

Yes, exactly I need total record count.

jlrdw's avatar

In your template, example:

@if ($paginator->hasPages())
    <ul class="pagination">
        {{-- Previous Page Link --}}
        @if ($paginator->onFirstPage())
            <li class="disabled"><span>{{ __('Prev') }}</span></li>
        @else
            <li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">{{ __('Prev') }}</a></li>
        @endif
        
        
        
        {{ "Page " . $paginator->currentPage() . "  of  " . $paginator->lastPage() }}
       
        
        {{-- Next Page Link --}}
        @if ($paginator->hasMorePages())
            <li><a href="{{ $paginator->nextPageUrl() }}" rel="next">{{ __('Next') }}</a></li>
        @else
            <li class="disabled"><span>{{ __('Next') }}</span></li>
        @endif
    </ul>
@endif

Replace this line with your line

{{ "Page " . $paginator->currentPage() . "  of  " . $paginator->lastPage() }}

Instead of using $paginator->lastPage()

use

$paginator->total()

note also

Use the template from the style needed, bootstrap, tailwind, plain, as laravel has several it comes with.

Publish the templates, or I usually just copy the one I need to modify. Don't modify anything in the vendor folder.

So answer is:

{{ "Showing from " .  $paginator->firstItem() . " to " . $paginator->lastItem() . " of " . $paginator->total() }}

All this was in the chapter on pagination.

Please or to participate in this conversation.