11

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
localheinz
  • 9,179
  • 2
  • 33
  • 44
RockNova
  • 161
  • 1
  • 1
  • 10

8 Answers8

29

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
Matt Kieran
  • 4,174
  • 1
  • 17
  • 17
8

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
Aman Maurya
  • 1,305
  • 12
  • 26
  • 2
    The keys might not be numeric, or not zero-indexed, and then the condition might evaluate to `true` even though it's the first element of the array. – localheinz Aug 20 '17 at 17:36
3

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

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
  • 1
    The keys might not be numeric, or not zero-indexed, and then accessing the elements from the array might fail. – localheinz Aug 20 '17 at 17:40
3

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

Mahmoud
  • 167
  • 2
  • 7
2

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
Pawel Bieszczad
  • 12,925
  • 3
  • 37
  • 40
1

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:

localheinz
  • 9,179
  • 2
  • 33
  • 44
  • This will work in some cases, but usually you are iterating trough laravel collection not array... – DokiCRO Dec 10 '17 at 18:56
0

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
Aum Zagri
  • 137
  • 1
  • 5
0

As of Laravel 5.3 you can use @continue($loop->first)

@foreach($aboutcontent as $about)
@continue($loop->first)

...

@endforeach