Laravel blade $ loop index and remaining, laravelremaining
Laravel 5.3 blade adds $ loop Variable
The document is as follows:
In Laravel 5.3,@foreach
Commands provide more powerful functions in each@foreach
You can call a new$loop
Variable. This variable isstdClass
The instance contains the metadata information of the current loop. Let's take a look at the attributes it provides:
- Index: A circular index starting from 1. 1 indicates that it is the first entry.
- Remaining: number of entries in the loop. For example, if the current number is the first of the three, 2 is returned;
- Count: Total number of items in a loop
- First: whether it is the first
- Last: whether it is the last one
- Depth: loop level
- Parent: If the loop is located in another @ foreach, the parent loop reference is returned; otherwise, null is returned.
Data = ['A', 'B', 'C', 'D'];
@ Foreach ($ data as $ v)
<P>
{$ Loop-> count} // output 4
{$ Loop-> index} // output 0, 1, 2, 3
</P>
@ Foreach ($ data as $ key => $ value)
<P>
@ If ($ loop-> first)
{$ Loop-> parent-> depth} // 1 returns the parent loop. The parent loop level is 1.
@ Elseif ($ loop-> last) // last
{$ Loop-> depth} // 2 Number of current loop layers 2
@ Else
{$ Loop-> remaining} // 2, 1
@ Endif
</P>
@ Endforeach
@ Endforeach
We found that $ loop-> index, $ loop-> remaining are counted from 0, not from 1.
TEST data = [1 => 'A', 2 => 'B', 3 => 'C ', 4 => 'D'];
The test results are consistent.
Let's take a look at the English documents later.