Find last iteration of foreach loop in laravel blade

后端 未结 5 1606
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 22:03

In blade template i use last() method to find last iteration of foreach loop:

@foreach ($colors as $k => $v)
   
5条回答
  •  日久生厌
    2021-02-06 22:45

    As for Laravel 5.3+, you can use the $loop variable

    $loop->last
    
    @foreach ($colors as $k => $v)
         @if($loop->last)
             // at last loop, code here
         @endif
    @endforeach
    

提交回复
热议问题