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 }}


No users found.



You can use this:

@forelse ($users as $user)

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


No users found.



Hope it will help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js