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

AliMalik's avatar

How to check If an array is not null in blade

I can check for a variable if it exists or not in blade like {{ $Variable or 'Default' }}

But how can i check if an array exists or it is null. What i am doing is passing an array of values to blade to populate a select box. But the array can be completely null due to back-end logic so what is the best way to check and iterate if array have values then iterate and pass as a third argument to Form::select otherwise pass null to select.

<label class="select">
                {{ isset($alCities) ? $alCities : ' ' }}
                    @foreach($alCities as $city)
                     <?php $cts[] = $city->jobAdALCity; ?>
                        @endforeach
            
            {!! Form::select('jobAdALCity[]',$cityArray,null,['id'=>'alCity','multiple']) !!}
      </label>  
0 likes
5 replies
taijuten's avatar
Level 10
<label class="select">
    @if(is_null($alCities))
        // whatever you need to do here
    @else 
        @foreach($alCities as $city)
                        <?php $cts[] = $city->jobAdALCity; ?>
                    @endforeach
    @endif
        {!! Form::select('jobAdALCity[]',$cityArray,null,['id'=>'alCity','multiple']) !!}
  </label> 
4 likes
jekinney's avatar

@AliMalik

Also you can use the count() > 0 if needed. Sometimes you might return an empty array that will pass the null check but no data.

1 like
pmall's avatar

@AliMalik you should update your backend to return empty array when there is no items. One less condition to handle :)

Please or to participate in this conversation.