Answers for "laravel view foreach index number"

PHP
0

laravel foreach loop index

@foreach ($items as  $item)
{{ $loop->index }}
@endforeach
Posted by: Guest on June-07-2021
1

Laravel foreach loop index in controller

//If you are working with a collection you can do something like this:
foreach($collection as $item) {
    if($collection->last() == $item) {
        // last iteration
    }
}

//If you are working with an array (it also works with collections) you can do this:
foreach($array as $item) {
    if(end($array) == $item) {
        // last iteration
    }
}
Posted by: Guest on June-11-2021

Browse Popular Code Answers by Language