I would like you to help me understand how the @forelse
is applied in laravel .
It is the same as a foreach, but in case the input parameter is empty, it will show what is between @empty
and @endforelse
.
The goal of @forelse
is to reduce the code needed to do:
@if ($users->count())
@foreach ($users as $user)
<p>Usuario {{ $user->id }}</p>
@endforeach
@else
<p>No hay usuarios.</p>
@endif
to this:
@forelse ($users as $user)
<p>Usuario {{ $user->id }}</p>
@empty
<p>No hay usuarios.</p>
@endforelse
In case you want to see how to compile Blade: link
With an example it is understood, simply it crosses all the values of the array / collection that you pass to it and if this empty it jumps the else.
@forelse($array as $objeto_individual)
<h1> {{ $objeto_individual }}</h1>
@empty
<h1> Sin valores </h1>
@forelse