如何在 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


慕尼黑5688855
浏览 75回答 2
2回答

精慕HU

您可以使用循环变量,如下所示://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

慕容森

您可以在 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
打开App,查看更多内容
随时随地看视频慕课网APP