How to display serial number in Laravel?

Do you want to show display serial number in Laravel application?

By adding an auto increment serial number in Laravel, you can help your admin and users to find and indicate any collection of data in the application.

In this article, we will show you how you can display Auto increment number in Laravel blade, So you can easily apply it to your Laravel 5, Laravel 6, Laravel 7, Laravel 8, and Laravel 9 application.

display serial number in foreach loop in the blade file in Laravel

Image Featured section

Why Show Dynamic number of rows in Laravel?

When you start a new Laravel application, the admin and the users can face problems finding all your content easily in the blade files. However, as your application grows, so will the number of records. This makes it difficult for admin and users to find your most important records.

A simple way to help people find Like if the admin wants to delete multiple records he can directly find the serial number of the collection rather than search by name or ID.

In this article, we’ll share 2 methods of displaying related posts, so you can choose whichever way you prefer.

  • Show auto-incrementing number in laravel foreach loop using {{ $loop->iteration }}
  • Show the dynamic number of rows in Laravel Blade using (firstItem() + $index)

Show auto-incrementing number in laravel foreach loop using {{ $loop->iteration }}

For an auto-increment of the serial number in the laravel blade file, we can use {{ $loop->iteration }}, the best thing about that method it will automatically detect the key and increment the serial numbers.

<thead>
   <th>Sr.no</th>
   <th>Name</th>
   <th>Email</th>
</thead>
<tbody>
   @foreach ($users as $user)
   <tr>
      <td>{{ $loop->iteration }}</td>
      <td>{{ $user->name }}</td>
      <td>{{ $user->email }}</td>
   </tr>
   @endforeach
</tbody>
</tbody>

But whenever we use pagination it will show the same serial number repetition on the next page, so we will go with the next step.

Show the dynamic number of rows in Laravel Blade using (firstItem() + $index)

Most developers use the second method (firstItem() + $index) because it will never affect your serial number increment to pagination or any previous or next page.

<thead>
   <th>Sr.no</th>
   <th>Name</th>
   <th>Email</th>
</thead>
<tbody>
   @foreach ($users  as $index => $user)
   <tr>
      <td>{{ $users->firstItem() + $index }}</td>
      <td>{{ $user->name }}</td>
      <td>{{ $user->email }}</td>
   </tr>
   @endforeach
</tbody>
</tbody>

For more magic visit : https://laravel.com/docs/9.x/blade#loops.

We hope that this article helped you learn how to display serial number in Laravel. You may also want to check out our guide on how to choose the best Laravel Repository Design Pattern platform to manage a Laravel application.

Read also : Change the button color based on the status using the Model in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com