簡體   English   中英

如何在 Laravel 的 foreach 循環中跳過第一項?

[英]How to skip first item in a foreach loop in Laravel?

我正在嘗試使用基於存儲在數據庫中的內容的內容填充我的網頁。 但是,我想跳過第一項; 我想從第二個項目開始循環。

我怎樣才能做到這一點?

@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 5.4 開始,每當你在刀片文件中使用foreachfor ,你現在都可以訪問$loop 變量 $loop 變量提供了許多有用的屬性和方法,其中一個在這里很有用,用於跳過第一次迭代。 請參閱下面的示例,這是實現與此處其他舊答案相同的結果的更簡潔的方法:

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

試試這個:

@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

假設$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 或刀片,但根據文檔,這應該是可行的

如果您想在刀片中執行此操作,則需要某種計數器:

<?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

或者,您可以在迭代之前從數組中刪除第一個元素:

@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

優點是您不需要任何條件來驗證您不是第一次迭代中的那個。 缺點是您可能需要同一視圖中的第一個元素,但我們從您的示例中不知道。

注意在將數據傳遞給視圖之前從數組中刪除第一個元素可能更有意義。

參考:

有兩種方法可以做到這一點:1-如果您的 $key 是數字,您可以使用:

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

2- 如果 $key 不是數字,則使用 @loop->first 作為條件

嘗試這個

@foreach($aboutcontent->slice(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM