Laravel Blade循环中未定义的变量$ loop

问题描述:

根据最新的laravel刀片文档( https://laravel.com/docs/5.3/blade 参见循环"),我可以"[...使用循环变量获取有关循环的有价值的信息".

according to the latest laravel blade documentation (https://laravel.com/docs/5.3/blade see "loops") I can "[...] use the loop variable to gain valuable information about the loop[...]".

我的laravel版本是最新的,但是在我的foreach循环中,我无法访问$ loop变量.它说未定义的变量$ loop".

My laravel version is up to date but inside my foreach loop I can't access the $loop variable. It says "undefined variable $loop".

示例:

@foreach( $values["rating"] as $rating )
@if( $loop->iteration == 3 )
-- do something --
@endif
@endforeach

有人知道解决方案吗?非常感谢!

Does anyone know a solution for this? Thank you so much!

升级到Laravel 5.3后,您需要重新保存视图以使$ loop变量生效,因为您可能正在查看缓存版本.我遇到了同样的问题,升级了,仍然有问题,重新保存了视图(修改后),然后$ loop起作用了.

After upgrading to Laravel 5.3, you'll need to re-save your views in order to see the $loop variable take effect as you may be viewing the cached versions. I had the same issue, upgraded, still had the issue, re-saved the view (after modifying) and then $loop worked.