在 Laravel 裡有提供很方便的檢查是迴圈裡面第一筆還是最後一筆資料的方法,只要在迴圈內使用 $loop 這個變數就可以了:

@foreach ($users as $user)
    @if ($loop->first)
        This is the first user.
    @endif

    @if ($loop->last)
        This is the last user.
    @endif

This is user {{ $user->name }}

@endforeach