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

这里的技术是共享的

You are here

Foreach loop@foreach @break @continue

Foreach loop@foreach @break @continue

 

Basic usage

By default, blade doesn't have @break and @continue which are useful to have. So that's included.

Furthermore, the $loop variable is introduced inside loops, (almost) exactly like Twig. So il shamelessly steal their description table:

VariableDescription
loop.index1The current iteration of the loop. (1 indexed)
loop.indexThe current iteration of the loop. (0 indexed)
loop.revindex1The number of iterations from the end of the loop (1 indexed)
loop.revindexThe number of iterations from the end of the loop (0 indexed)
loop.firstTrue if first iteration
loop.lastTrue if last iteration
loop.lengthThe number of items in the sequence
loop.parentThe parent context

Usage example

@foreach($stuff as $key => $val)
    $loop->index;       // int, zero based
    $loop->index1;      // int, starts at 1
    $loop->revindex;    // int
    $loop->revindex1;   // int
    $loop->first;       // bool
    $loop->last;        // bool
    $loop->even;        // bool
    $loop->odd;         // bool
    $loop->length;      // int

    @foreach($other as $name => $age)
        $loop->parent->odd;
        @foreach($friends as $foo => $bar)
            $loop->parent->index;
            $loop->parent->parentLoop->index;
        @endforeach
    @endforeach  

    @break

    @continue
@endforeach

来自 http://robin.radic.nl/blade-extensions/directives/foreach.html

普通分类: