blade loop variable code example
Example 1: laravel for loop
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
@foreach ($users as $user)
This is user {{ $user->id }}
@endforeach
@forelse ($users as $user)
{{ $user->name }}
@empty
No users
@endforelse
@while (true)
I'm looping forever.
@endwhile
Example 2: $loop variable laravel
@foreach ($pages as $page)
- {{ $loop->iteration }}: {{ $page->title }}
@if ($page->hasChildren())
@foreach ($page->children() as $child)
- {{ $loop->parent->iteration }}.{{ $loop->iteration }}:
{{ $child->title }}
@endforeach
@endif
@endforeach