Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using serial number with paginate

I am trying to retrieve serial number in the blade template with variable i:

    <?php $i=0;?>
    @foreach ($lists as $li)
    <tr><td><?php $i++;?>{{$i}}</td><td>{{$li->Name}}</td><td>{{$li->Telephone}}
    </td><td>{{$li->mobile}}</td>
    @endforeach
    {!! $lists->render() !!}

but I am using paginate in the controller:

$lists=telephone::orderBy('name')->simplePaginate(10);

So, the first page's serial number is ok. But for the second page i starts from 1 again.

like image 607
micky Avatar asked Mar 20 '26 16:03

micky


2 Answers

You can also use

@foreach($lists as $index => $list)
   {{$index + $lists->firstItem()}} //S.N
@endforeach

$index is the row number in that result set and $lists->firstItem() will get the result number of the first item in the paginated results.

like image 97
Anuj Shrestha Avatar answered Mar 22 '26 05:03

Anuj Shrestha


UPDATE:

The solution below starts from 0 instead of 1 for the first page. To start from 1:

$i = ($lists->perPage() * ($lists->currentPage() - 1)) + 1;

Try setting $i to $lists->perPage() * ($lists->currentPage() - 1)

$i = $lists->perPage() * ($lists->currentPage() - 1);
like image 30
Doom5 Avatar answered Mar 22 '26 06:03

Doom5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!