How is @forelse interpreted in laravel and what is it for?

1

I would like you to help me understand how the @forelse is applied in .

    
asked by lucho 10.07.2017 в 08:08
source

2 answers

4

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

    
answered by 10.07.2017 / 15:24
source
1

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
    
answered by 10.07.2017 в 08:48