Here’s how to check the first iteration in a Blade @foreach loop:
You can use Laravel’s built-in $loop variable:
@foreach ($all_categories_ as $vals)
<li class="nav-item" role="presentation">
<button
class="nav-link p-3 border-0 text-start lh-sm w-100 rounded-0 fs-6 fw-medium
{{ $loop->first ? 'active' : '' }}"
id="pills-navTab{{ $vals->id }}-tab"
data-bs-toggle="pill"
data-bs-target="#pills-navTab{{ $vals->id }}"
type="button"
role="tab"
aria-controls="pills-navTab{{ $vals->id }}"
aria-selected="{{ $loop->first ? 'true' : 'false' }}"
>
<span class="d-block lead-sm fw-bold">{{ $vals->category_name }}</span>
<span>Equipment Rental Services</span>
</button>
</li>
@endforeach
Explanation:
-
{{ $loop->first ? 'active' : '' }}: Adds theactiveclass only to the first item. -
aria-selected="{{ $loop->first ? 'true' : 'false' }}": Ensures accessibility for tab behavior.
Let me know if you also want to show the first tab content as active — I can help with that part too.
