3
votes

I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.

I've found some threads on stackoverflow solving this issue for Laravel 4

Where they say, you should solve it like this

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

or like this

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

The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.

I tried it like this

<?php $count = $pages->firstItem() + 1 ?>
@foreach ($pages as $page)
    @include('pages.singlepagebasic')
@endforeach

and like this

{{ $count = $pages->firstItem() }}
@foreach ($pages as $page)
    @include('pages.singlepagebasic')
@endforeach

//pages.singlebasic.blade.php

{{ $count ++ }}

but my site always displays only the number "2" instead of counting up. How do I achieve that?

4

4 Answers

3
votes

Just use this to get pages count

$players->lastPage()

or this to get items count

$players->total()

If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that

<?php $count++; ?>
{{ $count }}
2
votes

First of all get 100 players with paginate(100) method then you can get the index or count in the following way:

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

Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.

1
votes

If you want to use laravel 5 loop itration

`@foreach ($players as $player)
    <tr>
        <td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
    </tr>
@endforeach`

Hope this helps.

0
votes

What i used to do in order to avoid php tags in blade templates in 4.2 was something like that

@foreach ($players as $key => $player)
<tr>
    <td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
@endforeach

It's kinda strange but it will do the trick.