Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 pagination count

I have set a pagination in my specific view/site, and it works.

The problem is I have a php counter:

<?php $count = 0;?> 
   @foreach ($players as $player)
    <?php $count++;?>
    <tr>
    <td>{{ $count }}. </td> 

and whenever I switch pages, it starts from 1.

How could I change that?

like image 636
erm_durr Avatar asked Nov 30 '22 20:11

erm_durr


1 Answers

In order to achieve that, you need to initialize the value of counter:

<?php $count = (($current_page_number - 1) * $items_per_page) + 1; ?>

Notice I'm first subtracting 1 from current page, so the first page number is 0. Then I'm adding 1 to the total result, so your first item starts with 1, instead of 0.

Laravel Paginator provides a handy shortcut for that:

<?php $count = $players->getFrom() + 1; ?>
@foreach ($players as $player)
    ...

There are a few others that you can use as you like:

$players->getCurrentPage();
$players->getLastPage();
$players->getPerPage();
$players->getTotal();
$players->getFrom();
$players->getTo();
like image 138
vFragosop Avatar answered Dec 03 '22 22:12

vFragosop