I do not understand why empty says that the variable is not empty

2

There is a membership module where members of a church are registered. For this register will be shown on the main page the birthday of the day in a jQuery slide. But when you do not have a birthday boy, he'll show you something else. When you do not have a birthday partner with dd () (dump () end die ()) shows that the variable is empty so I thought I'd do it like this:

<div class="col-md-4 col-sm-4 widget footer-widget">
    @if(empty($aniversario))
    <h4 class="footer-widget-title">Redes Sociais</h4>
    <ul class="rslides">
        <li><img src="/imagens/oracao/oracao.jpg" alt="">
            <div>
                <center><h5></h5></center>
            </div>
        </li>
        <li>
    </ul>
    @else
    <h4 class="footer-widget-title">Aniversariante do Dia</h4>
    <ul class="rslides">
        @foreach($aniversario as $aniver)
        <li><img src="/imagens/membros/{{$aniver->imagem}}" alt="">
            <div>
                <center><h5>{{$aniver->nome}}</h5></center>
            </div>
        </li>
        @endforeach
        <li>
            <img id="aniversario" src="/imagens/aniversario/aniversario.jpg" alt="">
        </ul>
    @endif
</div>

Where the variable $ anniversary is empty it will show an image, otherwise it will loop with the birthday image of the day + another image of a parabem.

When I make sound if(isset($aniversario)) it enters the birthday loop, more when I change the date of birth of the member it does not show the other div with if (empty ($ anniversary)) does the same thing more to the contrary .

This image shows the empty array when there is no one making birthday in the day.

MoreifItesttoseeifitisempty,itsaysthatitisnotex:

@if(empty($aniversario)){{"vazio " }}
@else
    {{"nao esta vazio"}}
@endif

And it shows empty being it enter the else. Then you tell me: Then why do not you deny the condition. Also does not work.EX:

@if(!empty($aniversario)) // mesmo ! negando ele somente faz o contrario mais não mostra a outra imagem
    {{ "vazio " }}
@else
    {{"nao esta vazio"}}
@endif

You know what that can be. How to solve this?

    
asked by anonymous 22.09.2017 / 17:55

2 answers

6

The correct one would be:

@if(isset($aniversario) && $aniversario->count() > 0)

Because it is a collection of class Collection , and empty will not have the expected effect because, the variable is empty, as is a class everything changes, so check that the variable exists with isset () and then ask if it has items with the count () method.

22.09.2017 / 18:02
2

You can use the collection method isEmpty() , it would look like:

@if($aniversario->isEmpty())

Reference:

22.09.2017 / 20:22