Skip to content

Easy Way to use Serial Number with Laravel Pagination

Reading Time: < 1 minute

We all know Laravel is one of the best PHP frameworks with its extended features. It has a simple pagination option that can be easily integrated. But in some scenarios, we need serial numbers for the rows on the paginated results. Here is your simple hack for this problem.

use this code on Views:

<table>
<tr>
<th>Serial No</th>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$pagination=25;
  if(!isset($_GET['page']) OR $_GET['page']==1){
   $i=1;
  }
  else{
   $i=(($_GET['page']*$pagination)-($pagination-1));
  }
?>
@foreach ($records as $record)
  <tr>
   <td>{{ $i}}</td>
   <td>{{ $record->name }} </td>
   <td>{{ $record->age }} </td>
  </tr>
<?php $i++; ?>
@endforeach
</table>
See also  Freelancer VS Entrepreneur

Leave a Reply