Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How access to $loop variable in nested foreach blead

@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 access to $loop of any of foreach, that was used in code?

like image 701
Farshid Rezaei Avatar asked Jul 09 '17 18:07

Farshid Rezaei


2 Answers

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

like image 134
Hamoud Avatar answered Nov 10 '22 12:11

Hamoud


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

like image 41
Nurkartiko Avatar answered Nov 10 '22 10:11

Nurkartiko