I have a @foreach loop in the Blade template and need to apply special formatting to the first item in the collection. How do I add a conditional to check if this is the first item?
@foreach($items as $item)
<h4>{{ $item->program_name }}</h4>
@endforeach`
Laravel 5.3 provides a $loop
variable in foreach
loops.
@foreach ($users as $user)
@if ($loop->first)
This is the first iteration.
@endif
@if ($loop->last)
This is the last iteration.
@endif
<p>This is user {{ $user->id }}</p>
@endforeach