Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fetch iterate number in blades foreach loop over collection

is there any way to fetch iterate number in blades foreach loop over collection ? I'm going to use it for selected drop down input default value . (i don't want to use "form model binding")

i try thesis and they does not work, maybe cause thesis for getting index value in foreach loop over objects :

key($theArrayYouAreLoopingThrough)

{{ @index }}

@foreach ($athletes as $key=>$athlete)
   {{ ++$key }}
@endforeach

and so on.

like image 272
behzad m salehi Avatar asked Sep 10 '16 10:09

behzad m salehi


1 Answers

If you're using 5.3 you can use $loop variable.

For example, to get current iteration, you can use inside @foreach or @for construction:

$loop->iteration
like image 81
Alexey Mezenin Avatar answered Oct 20 '22 10:10

Alexey Mezenin