What is a blade command?
The blade template language of Laravel provides instruction functionality, which is actually a custom label--at the beginning of the @, usually used to control the structure. If you write a template with a blade, you will be familiar with the commands like @if, @foreach.
These directives are usually equivalent to the corresponding PHP code, such as @if (condition) equivalent to <?php if ($condition):
$loop variables
In Laravel 5.3, @foreach directives provide more powerful functionality, and a new $loop variable can be invoked in each @foreach loop body. The variable is a Stdclass instance that contains the metadata information for the current loop, let's take a look at the properties it provides:
Index: Circular index starting at 1, 1 means the first entry.
Remaining: The number of entries in the loop, such as the one currently in the 3, returns 2;
Count: Number of loops total entries
First: Whether it is the number one
Last: Whether it is the final
Depth: The level of the cycle
Parent: If the loop is in another @foreach, return the parent circular reference, or null
The following is a sample code:
<ul>
@foreach ($pages as $page)
<li>{{$page->title}} ({{$loop->index}}}/{{$loop->count}}) </li>
@endforeach
</ul>
If there is a nested loop, you can use depth to determine and obtain the appropriate information through the $loop Parent property:
<ul>
@foreach ($pages as $page)
<li >{{$loop->index}}: {{$page->title}}
@if ($page-> HasChildren ())
<ul>
@foreach ($page->children () as $child)
<li>{{$loop->parent->index}}. {{$loop->index}}:
{{$child->title}}</li>
@ Endforeach
</ul>
@endif
</li>
@endforeach
</ul>
The
is so simple and convenient.