2

I'm trying to make something similar to reddit (just the ranking idea in a list of rows) and want each row counted consecutively.

Example Reddit listing

I can do this on one page with the tag

{{ $loop->iteration }}

Unfortunately, when I go to the second page, as I am using pagination, the numbers start over at 1. However, I would like them to continue from the last number on the previous page.

Any idea how to fix this?

I appreciate your help! :)

3
  • 3
    Can't you just do e.g. {{ $loop->iteration + $perPage * ($currentPage - 1) }} or similar?
    – Joel Hinz
    Jul 30, 2017 at 13:50
  • Thanks @Joel Hinz. I liked your idea. It didn't give me the right answer straight away, but did give me an idea. Upon further searching, I found a similar question. Here: stackoverflow.com/questions/32247499/… Jul 30, 2017 at 14:08
  • Sorry guys for the duplicate question! Jul 30, 2017 at 14:08

7 Answers 7

12

you can use $loop->index instead of $loop->iteration in the following way

{{ ($players ->currentpage()-1) * $players ->perpage() + $loop->index + 1 }}
3

for inverse count with pagination

{{ ($players->total()-$loop->index)-(($players->currentpage()-1) * $players->perpage() ) }}

3

Another way : (change $users to your collection name)

{{ $loop->iteration + $users->firstItem() - 1 }}
0
2

We can use this method in paginator the first index can be retrieved from the ->firstItem(). Like

@foreach($homeworks as $homework)
<tr>
<td>{{ $homeworks->firstItem() + $loop->index }}</td>
</tr>
@endforeach
1

Use:

{{ $products->firstItem()+$loop->index }}
0

Sorry for the duplicate question and big thanks to @JoelHinz for the idea. Upon further searching, I found the correct answer.

Laravel 5.1 pagination count

<?php $count = 1; ?> 
@foreach ($players as $player)
    <tr>
        <td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
    </tr>
    <?php $count++; ?>
@endforeach
0

use firstItem() method in loop :

    @foreach($contents as $index=>$record  )
       <tr>
           <th scope="row">{!!  $contents->firstItem() +$index!!}</th>
       </tr>
   @endforeach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.