Aug 27, 2019
6
Level 10
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?
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
Please or to participate in this conversation.