Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 auto incrementing row number with pagination

Is there any way to get pagination pretty url or something like this in laravel 5.1?

I have 15 rows in per page. And I want to increment the row count number even on paginate.

<?php  $count = 1; ?>
<tr>     
    <th>No</th>
    <th>Year</th>
    <th>Action</th>
</tr>
@foreach($years as $year)
<tr>
    <td width="20%">{{ $count++ }}</td>
    <td width="50%">{{ $year->year }}</td>
    <td width="30%">
        <a href="{{ route('admin.year.edit', $year->id) }}" class="btn btn-info">Edit</a>
    </td>
</tr>
@endforeach

But when I goes to next page the $count var starts from beginning. How can I get $count = 16 and it will increment and so on?

like image 261
Ohidul Islam Avatar asked Dec 05 '22 01:12

Ohidul Islam


2 Answers

In Laravel 5.3 you can now use the $loop variable in your views. So you could do something like:

{{ (($results->currentPage() - 1 ) * $results->perPage() ) + $loop->iteration }}

in your blade templates now.

like image 84
Rody Molenaar Avatar answered Dec 06 '22 13:12

Rody Molenaar


You can use :

@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
like image 29
Mhd Syarif Avatar answered Dec 06 '22 13:12

Mhd Syarif