为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Laravel 中使用 2 foreach

如何在 Laravel 中使用 2 foreach

PHP
慕尼黑5688855 2023-10-22 21:12:01
如何在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


查看完整回答
反对 回复 2023-10-22
?
慕容森

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


查看完整回答
反对 回复 2023-10-22
  • 2 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信