简体   繁体   中英

How to skip first item in a foreach loop in Laravel?

I am trying to fill my webpage with content based on content stored in a database. However, I would like to skip the first item; I want to start looping from the second item.

How can I achieve this?

@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

As of Laravel 5.4, whenever you use foreach or for within blade files you will now have access to a $loop variable . The $loop variable provides many useful properties and methods, one of them being useful here, for skipping the first iteration. See the example below, which is a much cleaner way of achieving the same result as the other older answers here:

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

Try This :

@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

Assuming that the $aboutcontents is numeric array just use the good old fashioned for loop instead of your new fangled 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
}

Note: I have not used Larvel or blades but based on the docs this should be doable

You need some kind of a counter if you want to do that in blade:

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

EDIT:

I like the answer provided by Mark Baker in the comment better

@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

Alternatively, you can just remove the first element from the array before iterating:

@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

The advantage is that you do not need any conditions to verify you are not the in the first iteration. The disadvantage is that you might need the first element within the same view, but that we don't know from your example.

Note It might make more sense to remove the first element from the array before you pass on the data to the view.

For reference, see:

There is two way to do this: 1- if your $key is numeric you can use:

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

2- if a $key is not numeric use @loop->first as a condition

Try this

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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