For Else Loop in Laravel

What do you do when you need to show a loop in laravel blade with foreach , but the list might be empty ? You probably use if-else statement around it,right? There is a magic loop structure called forelse. here is how it works.

Instead of :

@if ($users->count())
  @foreach ($users as $user)

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

  @endforeach
@else

No users found.

@endif

You can use this:

@forelse ($users as $user)

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

@empty

No users found.

@endforelse

Hope it will help you.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website