How access to $loop variable in nested foreach blade
Asked Answered
E

2

13
@foreach($last_articles->chunk(3) as $row)
        @foreach($row as $last_article)
                @if($loop->index!=0) {{"in $loop"}} @endif
        @endforeach
        @if($loop->index!=0) {{"out $loop"}} @endif
@endforeach

how to access the $loop of any of foreach, that was used in the code?

Emmenagogue answered 9/7, 2017 at 18:20 Comment(0)
H
32

You could use $loop->parent to access the parent loop of the current loop.

Hemoglobin answered 9/7, 2017 at 18:26 Comment(0)
T
3

In my case (Laravel 6.8) $loop->parent looks like this

[
{"iteration":1,"index":0,"remaining":1,"count":2,"first":true,"last":false,"odd":true,"even":false,"depth":1,"parent":null},
{"iteration":2,"index":1,"remaining":0,"count":2,"first":false,"last":true,"odd":false,"even":true,"depth":1,"parent":null}
]

to access the parent index $loop->parent->index

Tweezers answered 24/6, 2020 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.