Limiting the results in Blade foreach loop
Asked Answered
S

6

20

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!

Scutt answered 28/10, 2015 at 14:12 Comment(4)
Is $element['subs'] an array or a collection?Telescopium
Return 10 results from the controller, don`t try to cut it up in the view, that seems like bad practice.Drippy
@TimLewis I disagree. Maybe there's no way to do this in blade (and that's fine) but there's nothing wrong with been able to do this within a template (considering that I might want to use the same array in another part of the template but with a different loop count). This feature should be available at least for convenience.Halloween
@Gboyega Well, there's always a @for($i = 0; $i < 10; $i++) ... @endfor that can be used, or a foreach with a count (like the answer below). My comment was simply a statement on the strangeness of query for every result, sending those to the view but then only using 10... Just seems odd.Drippy
C
19

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
Conah answered 28/10, 2015 at 14:19 Comment(1)
I'm not using the controllers at the moment, I'm just putting it inside the front-end. It does work, so thanks for that! I will look into the controller things and keep learning. Cheers though!Scutt
S
61

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
Seringapatam answered 24/5, 2017 at 9:45 Comment(2)
This is what makes a good programmer an experts :)) cleaner code == god saves a kitty :)Smelly
Nice it is very clean and understanding Thanks youGizzard
C
19

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
Conah answered 28/10, 2015 at 14:19 Comment(1)
I'm not using the controllers at the moment, I'm just putting it inside the front-end. It does work, so thanks for that! I will look into the controller things and keep learning. Cheers though!Scutt
K
9

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
Kaiserdom answered 17/8, 2017 at 15:34 Comment(0)
P
8

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
Poised answered 27/4, 2021 at 13:52 Comment(1)
Thank a lot. This is the best answerBeggar
W
7

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
Whitcher answered 15/8, 2018 at 8:57 Comment(2)
Where does $loop come from?Sidestroke
It is defined by blade. More info at laravel.com/docs/5.7/blade#the-loop-variableWhitcher
M
4

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>
Mainstream answered 20/8, 2020 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.