【问题标题】:How to skip first item in a foreach loop in Laravel?如何在 Laravel 的 foreach 循环中跳过第一项?
【发布时间】:2016-12-18 04:28:54
【问题描述】:

我正在尝试使用基于存储在数据库中的内容来填充我的网页。但是,我想跳过第一项;我想从第二个项目开始循环。

我怎样才能做到这一点?

@foreach($aboutcontent as $about)
<div class="col-md-4 text-center">
   <div class="thumbnail">
     <img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
        <div class="caption">
            <h3>{{ $about->aboutname }}</h3>
            <p>{{ $about->aboutinfo }}</p>
        </div>
   </div>
</div>
@endforeach

【问题讨论】:

标签: php laravel-5.1 blade


【解决方案1】:

如果你想在刀片中这样做,你需要某种计数器:

<?php $count = 0;?>
@foreach
    @if($count>1)
        <div class="col-md-4 text-center">
           <div class="thumbnail">
             <img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
                <div class="caption">
                    <h3>{{ $about->aboutname }}</h3>
                    <p>{{ $about->aboutinfo }}</p>
                </div>
           </div>
        </div>
    @endif
    $count++
@endforeach

编辑:

我更喜欢 Mark Ba​​ker 在评论中提供的答案

@foreach(array_slice($aboutcontent, 1) as $about)
<div class="col-md-4 text-center">
   <div class="thumbnail">
     <img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
        <div class="caption">
            <h3>{{ $about->aboutname }}</h3>
            <p>{{ $about->aboutinfo }}</p>
        </div>
   </div>
</div>
@endforeach

【讨论】:

    【解决方案2】:

    试试这个:

    @foreach($aboutcontent as $key => $about)
    @if($key > 0){ 
       <div class="col-md-4 text-center">
           <div class="thumbnail">
             <img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
                <div class="caption">
                    <h3>{{ $about->aboutname }}</h3>
                    <p>{{ $about->aboutinfo }}</p>
                </div>
           </div>
        </div>
    @endif;
    @endforeach
    

    【讨论】:

    • 键可能不是数字,或者不是零索引,然后条件可能评估为true,即使它是数组的第一个元素。
    【解决方案3】:

    假设 $aboutcontents 是数字数组,只需使用老式的 for 循环,而不是新奇的 foreach

    // Notice you start at 1 and your first
    // elem is 0 so... ta da... skipped
    @for ($i = 1; $i < count($aboutcontents); $i++){
        $about = $aboutcontents[$i]; //This is the object
    
        //now use $about as you would
    }
    

    注意:我没有使用过 Larvel 或刀片,但根据文档,这应该是可行的

    【讨论】:

    • 键可能不是数字,或者不是零索引,然后访问数组中的元素可能会失败。
    【解决方案4】:

    从 Laravel 5.4 开始,无论何时您在刀片文件中使用 foreachfor,您现在都可以访问 $loop variable。 $loop 变量提供了许多有用的属性和方法,其中一个在这里很有用,用于跳过第一次迭代。请参阅下面的示例,这是一种更简洁的方法,可以实现与此处其他较旧答案相同的结果:

    @foreach ($rows as $row)
        @if ($loop->first) @continue @endif
        {{ $row->name }}<br/>
    @endforeach
    

    【讨论】:

      【解决方案5】:

      或者,您可以在迭代之前从数组中删除第一个元素:

      @php
          array_shift($aboutcontent);
      @endphp
      @foreach($aboutcontent as $about)
      <div class="col-md-4 text-center">
          <div class="thumbnail">
              <img id="" class="img-responsive" src="images/{{ $about->aboutimg }}" alt="">
              <div class="caption">
                  <h3>{{ $about->aboutname }}</h3>
                  <p>{{ $about->aboutinfo }}</p>
              </div>
         </div>
      </div>
      @endforeach
      

      优点是您不需要任何条件来验证您不是第一次迭代。缺点是您可能需要同一个视图中的第一个元素,但我们从您的示例中不知道。

      注意在将数据传递给视图之前从数组中删除第一个元素可能更有意义。

      参考见:

      【讨论】:

      • 这在某些情况下会起作用,但通常你是通过 laravel 集合而不是数组进行迭代...
      【解决方案6】:

      有两种方法可以做到这一点: 1-如果您的 $key 是数字,您可以使用:

      @foreach($aboutcontent as $key => $about)
       @if($key == 0)
        @continue
       @endif
       code
      @endforeach
      

      2- 如果 $key 不是数字 使用@loop->first 作为条件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2019-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        • 1970-01-01
        相关资源
        最近更新 更多