我试图用类“活动”第一项来循环传送带。
这是我的密码
<div class="carousel-item active">
<div class="top-top">
<h4>some heading</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/ynK2WIRg?rel=0" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@foreach($test as $tes)
<div class="carousel-item">
<div class="top-top">
<h4>{{ $tes->name }}</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/{{ $tes->file_name }}?rel=0" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@endforeach
</div>我也试图将活动项放在循环中。
发布于 2020-12-08 10:41:14
您可以使用$loop->iteration==1,如在foreach中的laravel刀片医生中所说:
@foreach($test as $tes)
<div class="carousel-item @if($loop->iteration==1) active @endif">
<div class="top-top">
<h4>{{ $tes->name }}</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/{{ $tes->file_name }}?rel=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@endforeach发布于 2020-12-08 10:43:27
您可以在循环中使用回路变量:
@foreach($test as $tes)
<div class="carousel-item@if($loop->first) active@endif">
<div class="top-top">
<h4>{{ $tes->name }}</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/{{ $tes->file_name }}?rel=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@endforeach发布于 2020-12-08 10:47:06
您可以使用循环变量
在您的循环中,您可以访问许多有用的信息,例如如果是第一个或最后一个循环,当前循环计数,如果偶数或奇数.在这里,您可以在官方文档中找到所有的信息。
https://laravel.com/docs/8.x/blade#the-loop-variable
在你的例子中,你可以这样做:
@foreach($test as $tes)
@if ($loop->first)
<div class="carousel-item active">
<div class="top-top">
<h4>{{ $tes->name }}</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/{{ $tes->file_name }}?rel=0" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@else
<div class="carousel-item">
<div class="top-top">
<h4>{{ $tes->name }}</h4>
<iframe class="testimonial" width="100%" height="auto"
src="https://www.youtube.com/embed/{{ $tes->file_name }}?rel=0" frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
</iframe>
</div>
</div>
@endif
@endforeachhttps://stackoverflow.com/questions/65197302
复制相似问题