简体   繁体   English

如何从foreach循环中仅将一项放入某项(使用laravel)

[英]how to put something in only 1 item from a foreach loop (using laravel)

I am trying to give my first item a class active in this loop. 我正在尝试为我的第一项提供一个在此循环中处于活动状态的类。 But the second and third item may not have class 'active'. 但是第二和第三项可能没有“活动”类别。

            @foreach ($news as $newsitem)
                <div class="item">
                    <div style="background:url({{$newsitem->media->file_url}}) center center; 
                              background-size:cover;" class="slider-size">
                        <div class="carousel-caption">
                          <h2>{{$newsitem->title}}</h2>
                          <p>{{ Str::limit($newsitem->content, 30)}} </p>
                        </div>
                      </div>
                </div>
            @endforeach

You can use $key => $val and check if $key iterator is 0 : 您可以使用$key => $val并检查$key迭代器是否为0

@foreach ($news as $key => $newsitem)
  <div class="item {{ $key === 0? 'active' : '' }}">
    <div style="background:url({{$newsitem->media->file_url}}) center center; 
                background-size:cover;" class="slider-size">
      <div class="carousel-caption">
        <h2>{{$newsitem->title}}</h2>
        <p>{{ Str::limit($newsitem->content, 30)}} </p>
      </div>
    </div>
  </div>
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM