如何在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
2 回答
精慕HU
TA贡献1845条经验 获得超8个赞
您可以使用循环变量,如下所示:
//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
慕容森
TA贡献1853条经验 获得超18个赞
您可以在 Blade 中将帖子数据分成 2块
@foreach($posts->chunk(5) as $chunk)
@foreach($chunk as $post)
//.....
@endforeach
//add your banner here after first chunk is rendered
@endforeach
或者单个循环并检查当前迭代
@foreach($posts as $post)
//...........
@if($loop->iteration == 5)
//add your banner here
@endif
@endforeach
- 2 回答
- 0 关注
- 112 浏览
添加回答
举报
0/150
提交
取消