Find last iteration of foreach loop in laravel blade

In Laravel Blade, you can use the @forelse loop to check if the last iteration of a loop has been reached. The @forelse loop works like a regular @foreach loop, but it also provides an @empty directive to handle empty collections, and an @endforelse directive to mark the end of the loop.

Watch a course Learn object oriented PHP

Here's an example of how you would use the @forelse loop in a Blade template:

@forelse ($items as $item)
    <!-- Display the item -->
    {{ $item->name }}

    <!-- Check if this is the last iteration -->
    @if ($loop->last)
        <!-- Do something on the last iteration -->
    @endif
@empty
    <!-- Display a message if the collection is empty -->
    <p>No items found.</p>
@endforelse

In the example above, the $loop->last variable will be true on the last iteration of the loop, so you can use an @if statement to check for this and do something specific on the last iteration.

Alternatively you can use @if ($loop->iteration === $loop->count) to check if the current iteration is equal to the total number of iterations.