Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

increment row number with laravel pagination

How to make increment row number with laravel pagination ? When i use pagination and i go to page 2 and above it will back to beginning. for example i will paginate(3)

    <thead>
    <tr>
       <th>No</th>
       <th>Name</th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; ?>
    @foreach ($telephone->results as $telp)
    <tr>
       <td>
         {{$i++}}
       </td>
       <td>{{ $telp->name }}</td>                               
    </tr>
    @endforeach
    </tbody>

when i go to page 2 the number will start from 1 again.

i need to make it when i go to page 2 it will start from 4

like image 263
user2194246 Avatar asked Aug 02 '13 18:08

user2194246


6 Answers

In Laravel 5.3 you can use firstItem():

@foreach ($items as $key => $value)    
  {{ $items->firstItem() + $key }}
@endforeach
like image 63
o.v Avatar answered Nov 19 '22 20:11

o.v


The below works with laravel 5.4

<?php $i = ($telephone->currentpage()-1)* $telephone-
>perpage() + 1;?>
@foreach($telephone as $whatever)
<td> {{ $i++ }}</td>
@endforeach

Edited The below works for laravel 5.7 above

@foreach ($telephone as $key=> $whatever)
  <td>{{ $key+ $telephone->firstItem() }}</td>
@endforeach
like image 41
Sodruldeen Mustapha Avatar answered Nov 19 '22 21:11

Sodruldeen Mustapha


You should be able to use the getFrom method to get the starting number of the current pages results. So instead of setting $i = 1; you should be able to do this.

<?php $i = $telephone->getFrom(); ?>

In Laravel 3 there is no getFrom method so you need to calculate it manually.

<?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
like image 8
Jason Lewis Avatar answered Nov 19 '22 21:11

Jason Lewis


Laravel 5.3

@foreach ($products as $key=>$val)
  {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
@endforeach
like image 7
Mhd Syarif Avatar answered Nov 19 '22 20:11

Mhd Syarif


For Laravel 6.2: $loop - just in case, is a built-in instance in Blade

@foreach($array as $item)
    <tr class="table-row">
      <td class="site-id">
         {{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
      </td>
    </tr>
@endforeach
</table>
like image 4
Alex Gore Avatar answered Nov 19 '22 19:11

Alex Gore


You can simply add the following line

$i = ($telephone->currentpage()-1)* $telephone->perpage();

in place of

$i = 1;
like image 1
Shweta Avatar answered Nov 19 '22 20:11

Shweta