You can do something like this:
@foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
@endforeach
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
<?php $pos=1 ?>
@foreach ($athletes as $athlete)
//some html goes here
<?php print $pos++ ?>
@endforeach
You can do something like this:
@foreach ($athletes as $key=>$athlete)
// Some html goes here
{ { ++$key } }
@endforeach
Thanks you very much :)
@krabeats There is a loop variable
@MichalOravec Never saw this, i own you a drink. :P
if $athletes is collection object u can simply use this function. $athletes->count();
You can try below code with $indexKey
@foreach ($athletes as $indexKey => $athlete)
{{$indexKey}}
@endforeach
$indexKey starting with 0, So it should be {{ $indexKey+1 }}
Might be late but with Laravel 5.3 I think, Blade templating, you now have a $loop variable inside a loop which is an object of useful properties.
$loop->index returns the current loop index starting from 0
$loop->iteration starts from 1
This was helpful Thanks @paschal
See https://laravel.com/docs/5.4/blade#loops
@foreach ($athletes as $athlete)
{{-- start the marathon or jump off the plane... --}}
{{ $loop->iteration }} {{-- Starts with 1 --}}
@endforeach
what if you have you have an if statement
@foreach ($athletes as $athlete)
@if ($athlete->name != 'blah blah')
@endif
@endforeach
please read this https://laravel.com/docs/5.4/blade#loops, for me $loop->iteration gets the job done. This increases with the iteration starting from 1.
@foreach ($Users as $user)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$user->receiver}}</td>
<td>{{$user->receiver_package}}</td>
<td>{{$user->payer}}</td>
<td>{{$user->payer_package}}</td>
<td>{{$user->created_at}}</td>
</tr>
@endforeach
@codeNfold What if i am using another loop inside this loop, it will then give me repeated iterations again and again. How to resolve it?
@azzamumer77 Why have you replied to a comment that is half a decade old?
You would have gotten the answer to your question if you had just read the documentation:
https://laravel.com/docs/9.x/blade#the-loop-variable
If you are in a nested loop, you may access the parent loop's
$loopvariable via theparentproperty:
I try this:
In Controller: $author = Author::findOrFail($id);
In View: @foreach ($author->books as $book) @endforeach
Amount of books by this author: {{ count($author->books) }}
@THOMASKIM - thank you !
You can use: $loop->iteration It already counts each iteration.
Please or to participate in this conversation.