Laravel Blade Template – How to use incremental variable in foreach loop

Solution:1

Try this $loop->iteration

 @foreach($rows as $row)
            <tr>
                <td>{{ $loop->iteration }}</td>
                <td>{{ $row->item1}}</td>
                <td>{{ $row->item2}}</td>
            </tr>
 @endforeach

The result will be like this

---------------------------------
Si#     |    item 1    |  item2 |
---------------------------------
1       |    AAA       |  111   |
2       |    BBB       |  222   |
---------------------------------

Solution:2

If you are using laravel > 5.4 then you can use $loop variable to get the index.

@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>This is user {{ $user->id }}</p>
@endforeach

Similarly, you can apply no need to reinvent the wheel.

@foreach($rows as $row)
   //do something

 {{ $loop->index }} 
@endforeac

hope this helps