I don’t understand why Empty says the variable is not empty

Asked

Viewed 1,485 times

2

There is a module of Members, where members of a church are registered. By this registration will be shown on the main page the day’s birthday on a jQuery slide. But when he doesn’t have a birthday boy, he’ll show something else. When there is no birthday with dd() (dump() end die()) shows that the variable is empty then I thought to do so see:

<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 if the $birthday variable is empty it will show an image, if not it will loop with the day birthday pictures + another picture of a birthday.

when I make sound if(isset($aniversario)) he enters the birthday loop, but when I change the birth date of the member he doesn’t show the other div with if(Empty($birthday)) does the same thing more the other way around.

This images shows the empty array when no one is doing birthday on the day. quando nao tem aniversariante

Plus if I do a test to see if it’s even empty he says it’s not ex:

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

And it shows being empty he enters Isis. Then you tell me: So why don’t you deny the condition. It doesn’t work either.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 this can be. How to solve this?

2 answers

6


The right thing would be:

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

because it’s a class collection Collection, and the empty will not have the expected effect, because, it checks if the variable is empty, as it is a class everything changes, then check if the variable exists with isset() and then ask if it has items with the method Count().

References:

  • 1

    Truth man. Very obg. Relamente. had to check with the same Count. Plus in vdd it didn’t even cross my mind. Ball show.

2

You can use the method of the own isEmpty(), would be:

@if($aniversario->isEmpty())

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.