首页 > 解决方案 > 当我在 laravel 的刀片中使用 foreach 时,last() 或 end() 不起作用

问题描述

end()last()不工作...怎么办?

@foreach($students as $student)

@foreach($payment_heads as $payment_head)

      <td>

      @foreach ($student->payment_detail as $payment_detail_one)


           @if($payment_detail_one->payment_head_id==$payment_head->id)
              <?php

                $total=$total+$payment_detail_one->amount;

              ?>

              @if($payment_detail_one === end($$student->payment_detail)) 
             {{$payment_detail_one->amount}}
             @endif

          @endif

      @endforeach
  </td>
  @endforeach
 @endforeach

标签: phplaravel-5.4

解决方案


请参阅文档https://laravel.com/docs/5.5/blade#the-loop-variable
end($$student->payment_detail)用了 double$$
你可以用这个

@foreach($students as $student)
    @foreach($payment_heads as $payment_head)
        <td>
            @foreach ($student->payment_detail as $payment_detail_one)
                @if($payment_detail_one->payment_head_id==$payment_head->id)

                    <?php
                        $total = $total + $payment_detail_one->amount;
                    ?>

                    @if ($loop->last)
                        {{$payment_detail_one->amount}}
                    @endif

                @endif
            @endforeach
        </td>
    @endforeach
@endforeach

推荐阅读