The first thing this post will do is show an example of a lambda foreach loop. I’ll start by providing a straightforward example and solution if you have any questions regarding the Laravel foreach loop. I’d like to present to you the first piece of the Laravel Blade. You will get a basic example of a laravel blade foreach initial thing in this tutorial.
A @foreach loop is provided by Laravel Blade to render array data. To apply a class or take other appropriate action based on whether an element is the first or last, you can utilize $loop->first and $loop->last.
some basic examples of how to use $loop->first and $loop->last in your blade file.
Example 1:
@foreach ($users as $user)
@if ($loop->first)
This is the first item.
@endif
@if ($loop->last)
This is the last item.
@endif
<p>This is user {{ $user->name }}</p>
@endforeach
Example 2:
<ul>
@foreach ($users as $user)
<li @if ($loop->first) class="active" @endif>
{{ $user->name }}
</li>
@endforeach
</ul>
Example 3:
<ul>
@foreach ($users as $user)
<li @if ($loop->last) class="active" @endif>
{{ $user->name }}
</li>
@endforeach
</ul>
[…] Example of Laravel Blade Foreach First Element […]