首页 > 解决方案 > 如何在 laravel 中使用 2 foreach

问题描述

如何在 laravel 中使用 2 foreach?

横幅 div 不能包含在 forach 中。因为布局被破坏了。

//for show post 1-5
@foreach ($posts as $post)
    //.................
@endforeach

//for banner
<div>
...............
</div

//continue foreach (for show 6-...)
@foreach ($posts as $post)
    //.................
@endforeach

标签: phplaravel

解决方案


您可以使用循环变量,如下所示:

//for show post 1-5
@foreach ($posts as $post)
    @if($loop->index < 5)
        // Your code
    @endif
@endforeach

//continue foreach (for show 6-...)
@foreach ($posts as $post)
    @if($loop->index > 5)
        // Your code
    @endif
@endforeach

推荐阅读