What is the Difference between foreach and forelse in Laravel
You probably came across the @forelse statement in Laravel and you are asking yourself what it is.
Well briefly @foreach in blade Laravel statement is used when you are trying to loop over an array of things, Say you have data for users stored in a variable called $users and you need to go over them the proper way will be doing that using the coding snippet below
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
But let's say you have no data ?! the array you are looping through is empty what do you do then?
I have experienced this several times when I was looping over a data table showing the various amounts of data. But when there is no data to present the table just went empty. So @forelse solves this by having an empty statement scope that allows you to write something when there is no data.
See the code snippet below
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
Hope this helps if you need any help in terms of Laravel tips send me a mail through epmnzava@gmail.com