欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel blade.php @foreach break 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!

shareimprove this question
 
1 
Is $element['subs'] an array or a collection? – Joseph Silber Oct 28 '15 at 14:13
2 
Return 10 results from the controller, don`t try to cut it up in the view, that seems like bad practice. – Tim Lewis Oct 28 '15 at 14:13
   
@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.– Gboyega Aug 19 '16 at 15:18
   
@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. – Tim Lewis Aug 19 '16 at 16:38

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
shareimprove this answer
 
   
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! – Stefan Neuenschwander Oct 28 '15 at 14:27

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



来自 https://stackoverflow.com/questions/33393376/limiting-the-results-in-blade-foreach-loop

I want to for loop 7,and foeach $time to blade.

if has $time foreach = for loop $i is output <div>O</div>, else is output<div>X</div>

but my code is trouble... loop 35 time.

I want loop total maximum time of 7 times

if $time = [3,6]

example output  :X X X O X X O

or if $time = [ 1 , 2 , 4]

example output :  X O O X O X X

How can I do, Please help me, thanks~

blade.php

@for ($i = 0; $i < 7; $i++)
@foreach($time as $value)

@if($value->time == $i)
  <div>O</div>
@else
  <div>X</div>   
@endif

@endforeach
@endfor

Controller

public function interview()
{
    $time = Interview_time::where('bsinformations_id',5)->get();
    return view('bs_sidebar.interview_time', [
        'time' => $time
    ]);
}
shareimprove this question
 
   
Try using @continue; and removing the else statement (which is why your loop executes 35 times). Move the <div>X</div> outside the foreach, remove the else and replace it with @continue;. This will function as you want. I've constructed this to show the idea, hope it helps. – camelCase Oct 11 '16 at 4:52
   
@camelCase - I might be wrong, but won't that output X on each for iteration, regardless if it outputted 0 inside the foreach? – Magnus Eriksson Oct 11 '16 at 4:57
   
@MagnusEriksson yes, but it's only reached if the $value->time !== $i due to the @continue;. I think this is what the OP is looking for but not quite sure. – camelCase Oct 11 '16 at 4:58
   
@camelCase - I still think you will get a X too many. @Bruce, what is the point of this loop and values? There must be another way to do what you are trying to do? What does the $time contain and ... well... what are you trying to solve? – Magnus Eriksson Oct 11 '16 at 5:02
   
@MagnusEriksson you might be right...but I've made a little fiddle that spits out what is listed in the question correctly...however as you're stating, it might not actually be what the OP is truly looking for. – camelCaseOct 11 '16 at 5:04

You can try this way:

@for ($i = 0; $i < 7; $i++)
    $check = 0;
    @foreach($time as $value)

        @if($value->time == $i)
            <?php
                $check = 1;
            ?>
            @break 
        @endif

    @endforeach
    @if $check == 1
        <div>O</div>
    @else
        <div>X</div>
    @endif
@endfor

so this way, the loop will only run 7 times,

please give it a try and let me know if it helps you

Thanks.

shareimprove this answer
 
   
thank you answer, I try this bencomeau.com/projects/phpFiddle/fiddle.html#QRQAIFRf the loop will only run 7 times, but foreach only loop 1 times, other value is not view – Bruce Oct 11 '16 at 6:06
   
if $time = [ 1 , 2 , 4] example output : X O O X O X X – Bruce Oct 11 '16 at 6:12
   
i have updated my answer, now check, this must help you – Insomania Oct 11 '16 at 6:13
   
Thanks your reply! bencomeau.com/projects/phpFiddle/fiddle.html#MJWWHbai but foreah 1 others all change O.. – Bruce Oct 11 '16 at 6:27
   
yes there was a mistake, sorry for that, just updated it again, now check :) – Insomania Oct 11 '16 at 6:28
   @for ($i = 0; $i < 7; $i++)
@foreach($time as $value)
    @if($value['time'] == $i)
        <?php
            $flag = 0;
        ?>
        @break
    @else
        <?php
        $flag = 1;
        ?>
    @endif
    @break
@endforeach
@if($flag==0)
    <div>O</div>
@else
    <div>X</div>
@endif
@endfor
shareimprove this answer
 
   
Agarwala thanks your answers! but foreach $time is only loop 1 times, others value is not loop show. – Bruce Oct 11 '16 at 6:10
   
 
普通分类: