Laravel blade $ loop index and remaining, laravelremaining

Source: Internet
Author: User

Laravel blade $ loop index and remaining, laravelremaining

Laravel 5.3 blade adds $ loop Variable

 

The document is as follows:

In Laravel 5.3,@foreachCommands provide more powerful functions in each@foreachYou can call a new$loopVariable. This variable isstdClassThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.