Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgrim's avatar

Issue with Isset and if statements in blade file

Hi I am trying to do an if statement but with a variable that may or may not be there. I tried using

@isset($zipreturns)
                @foreach($zipreturns as $zipreturn)
                    <div>
                        {!!$zipreturn->returns!!}
                    </div>
                @endforeach
            @endisset

but I couldn't figure out how to do an @else if there wasnt a zip return. So I looked online and that suggested

@if (count($zipreturns) > 0)
   @foreach($zipreturns as $zipreturn)
<div>
{!!$zipreturn->returns!!}
</div>
@endforeach
@else
nothing here
@endif

This worked great locally but as soon as I uploaded to my server it gives me the error "Undefined variable: zipreturns.

How would I do an @else statement with isset?

0 likes
6 replies
uksarkar's avatar

Hello @chrisgrim do it,

@isset($record)
    @foreach($zipreturns as $zipreturn)
    <div>
    {!!$zipreturn->returns!!}
    </div>
    @endforeach
@else
    nothing here
@endisset

It will solve your problem.

shez1983's avatar

you can always go back to basic

@if(isset()

@else 

@endif
Snapey's avatar
Snapey
Best Answer
Level 122

I usually find null coalesce operator works wonders!

eg

@forelse ($zipreturns ?? [] as $zipreturn)
    <div>
        {!!$zipreturn->returns !!}
    </div>
@empty
    <p>No zipreturns</p>
@endforelse

2 likes
chrisgrim's avatar

That last one worked wonders, though I don't really understand it. I'll have to do some more research.

Snapey's avatar

The null coalesce operator returns the [] if the first parameter is null (or missing)

so, you effectively end up with an empty array instead of an error

as if it were @forelse ([] as $zipreturn)

1 like

Please or to participate in this conversation.