Mithridates's avatar

laravel pagination showing current items count numbers

I have implemented pagination successfully.
Right now according to current page I wanna show a sentence like: Showing 15 to 30 of 57 entries which 15 is starting items number in table
30 is last item number
and 57 is all items count
Does anybody know how I get this

0 likes
8 replies
Mithridates's avatar
Mithridates
OP
Best Answer
Level 7

Just use below code

<div>Showing {{($users->currentpage()-1)*$users->perpage()+1}} to {{$users->currentpage()*$users->perpage()}}
    of  {{$users->total()}} entries
</div>
9 likes
azer's avatar
Showing {{($users->currentpage()-1)*$users->perpage()+1}} to {{ $users->currentpage()*(($users->perpage() < $users->total()) ? $users->perpage(): $users->total())}} of {{ $users->total()}} entries
2 likes
mandys's avatar

The above will not work if you have lets say only 5 entries on the last page and entries set per page to 10.

It will say showing 21 to 30 of 25 entries.

mandys's avatar

You could instead use:

Showing {{($users->currentpage()-1)$users->perpage()+1}} to {{(($users->currentpage()-1)$users->perpage())+$users->count()}} of {{$users->total()}} entries

3 likes
4ndro1d's avatar

@mandys You missed a * as multiplicator (it throws an syntax error for me. So to show current entries max use this:

{{($users->currentpage()-1) * $users->perpage() + $users->count()}}

2 likes
amitshahc's avatar

@Mithridates answer worked but wondering how come {{($shops->currentpage()-1)*$shops->perpage()+1}} evaluates. because if current page is 1 then it will be something (1-1) i.e. 0*anything = 0

amitshahc's avatar

Update:

@Mithridates why so complex logic when it can be obtain by direct object members $shops->firstItem() and $shops->lastItem()

Also, in case of total 9 records it will show wrong in last page Showing 6 to 10 of total 9 entries

Showing {{ $shops->firstItem() }} to {{ $shops->lastItem() }}
of total {{$shops->total()}} entries
9 likes
abgran's avatar

Just use this code it's working :

Showing {{ $shops->firstItem() }} to {{ $shops->lastItem() }} of total {{$shops->total()}} entries

2 likes

Please or to participate in this conversation.