Foreach && Forelse in Laravel

If you are a developer who have been using laravel then you would have used foreach a lot of times while fetching data array coming from . foreach is a loop that runs for all the data in the array.

@foreach($data as $data)
  <div>{$data->name}</div>
@endforeach

Code Explanation:
@foreach($data as $data), it renders all the data coming from data variable and

But What If, the data i.e Array is empty or there are no data available in the array $a=[]. In such cases we have forelse where it work similar to foreach but also gives us a condition when the array is empty or there are no data available. It gives us a condition @empty which we could use when the array is empty and have to show or the condition we need if the data in the array is empty.

@forelse($data as $dataItem)
  <div>
    {{$dataItem->name}}
  </div>
@empty
  <span>
    There are no more data
  </span>
@endforelse