Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在foreach循环中仅将某个内容放入一个项目中(使用laravel)_Php_Html_Laravel_Foreach - Fatal编程技术网

Php 如何在foreach循环中仅将某个内容放入一个项目中(使用laravel)

Php 如何在foreach循环中仅将某个内容放入一个项目中(使用laravel),php,html,laravel,foreach,Php,Html,Laravel,Foreach,我试图给我的第一个项目一个在这个循环中活动的类。但第二和第三项可能没有“活动”类 @foreach ($news as $newsitem) <div class="item"> <div style="background:url({{$newsitem->media->file_url}}) center center;

我试图给我的第一个项目一个在这个循环中活动的类。但第二和第三项可能没有“活动”类

            @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
@foreach($news作为$newsitem)
{{$newsitem->title}
{{Str::limit($newsitem->content,30)}

@endforeach
您可以使用
$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
@foreach($news as$key=>$newsitem)
{{$newsitem->title}
{{Str::limit($newsitem->content,30)}

@endforeach