Stefan Neuenschwander
Stefan Neuenschwander

Reputation: 833

Limiting the results in Blade foreach loop

Alright so I'm pretty new to Blade, and I did manage to get all the results that I asked for on my page. Now I want to show only 10 of the total items on my page and I seem to struggle with it, tried the array_slice without any success so far. Any suggestions?

Below the code I'm currently using

        {{--@foreach ($element['subs']->slice(0, 10) as $item)--}}

@foreach ($element['subs'] as $item)
        <div class="highlight {{ $element['class'] }}">
            <div class="el-inner-news">

                <div class="image-news">
                    <a href="{{ $item['news-item']['slug'] }}"> <img src="{{ $item['news-item']['image'] or "/assets/frontend/baywest/images/newsholder.png" }}" class="center-img" alt="{{ $item['news-item']['title'] }}" /> </a>
                </div>

                <div class="desc-news">
                    <div class="title-highlight">
                        <a href="{{ $item['news-item']['slug'] }}">{{ $item['news-item']['title'] }}</a>
                    </div>

                    <div class="text-highlight">
                        {!! $item['news-item']['textfield'] !!}
                    </div>

                    <div class="learn-more-news">
                        <a href="{{ $item['news-item']['slug'] }}">{{ $item['news-item']['read-more'] or "Learn more" }}  </a>
                    </div>
                </div>

            </div>
        </div>
    @endforeach

Thanks in advance!

Upvotes: 20

Views: 50868

Answers (6)

Gass
Gass

Reputation: 9364

As far as I know the easiest way to do this is by using the take(N) function, where N stands for the number of loops.

@foreach($element->take(5) as $item)
  ..your code
@endforeach

Example

@foreach($posts->take(5) as $post)
  {{ $post->message }}
@endforeach

Upvotes: 8

Sahilbalgotra100
Sahilbalgotra100

Reputation: 440

Take method help you

 <div class="col-md-4" style="margin-top:40px; margin-bottom:40px">
   @foreach($reviews->take(10) as $review)
      <div  class="carousel-cell">
           {{ $review->id }}
      </div>
   @endforeach
</div>

Second Way

<div class="col-md-4" style="margin-top:40px; margin-bottom:40px">
   @foreach($reviews as $count => $review)
    @if($count< 10)
      <div  class="carousel-cell">
           {{ $review->id }}
      </div>
    @endif
   @endforeach
</div>

Upvotes: 4

Adrian Hernandez-Lopez
Adrian Hernandez-Lopez

Reputation: 553

Since Laravel 5.3 there is a blade way to do this by using the Loop variable and the break directive:

@foreach ($element['subs'] as $item)

  @if($loop->iteration > 10)

    @break

  @endif

  {{-- Your loop code here --}}

@endforeach

Upvotes: 7

rocambille
rocambille

Reputation: 15976

Late, but to extend Pawel Bieszczad's answer in laravel 5.4 you can use the index property of the loop variable:

@foreach ($element['subs'] as $item)
    @if($loop->index < 10)
        // Your code
    @endif
@endforeach

Upvotes: 9

Mrquestion
Mrquestion

Reputation: 809

A cleaner way to do it could be this if it is a collection:

@foreach ($element['subs']->slice(0, 10) as $item)
 ...Code
@endforeach

another way for collections:

@foreach ($element['subs']->take(10) as $item)
 ...Code
@endforeach

or this if it is an array:

@foreach (array_slice($element['subs'], 0, 10) as $item)
 ...Code
@endforeach

Upvotes: 62

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13325

You should limit the results in controller, but here's how you can accomplish this in a blade. Not pretty.

<?php $count = 0; ?>
@foreach ($element['subs'] as $item)
    <?php if($count == 10) break; ?>
    // Your code
    <?php $count++; ?>
@endforeach

Upvotes: 19

Related Questions