Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Trying to access array offset on the value of type null in Laravel

Trying to access array offset on the value of type null in JSON Laravel

Do you want to solve the common error of Trying to access an array offset on the value of type null in Laravel?

Trying to access array offset on the value of type null in Laravel

Need to Apply condition on Json if the key has no image and an empty array in Laravel

In this example, we will implement JSON for storing multiple images into a single column.

In the blade file, when we decode JSON values, there is a common error “Trying to access array offset on the value of type null in Laravel” This error happens only whenever we have an empty JSON value, to protect and solve this error, we apply condition on JSON if the key has no image and an empty array in the blade file in Laravel.

Recommended article:- JSON Encode and Decode in Laravel.

Need to Apply condition on Json if the key has no image and an empty array in Laravel

Fixed JSON array offset on the value of type null Laravel in the Blade File

<div class="row g-2">
   @php $json =  json_decode($variable['images'], true); @endphp
   
   @if(!empty($json))
   @foreach($json as $media)
   <div class="col"><img src="{{ asset('storage')}}/{{ $media  ?? '' }}">
   </div>
   @endforeach
   @else
   No image found!
   @endif
</div>

I hope that this article helped you learn an example of a common error “Trying to access array offset on the value of type null in Laravel”, with the help of an example.

You may also want to check out our guide on Display hasMany relationships in a Blade File in Laravel.