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

DavP2021's avatar

Laravel 8 (PHP 7.4) Trying to access array offset on value of type null

I have the above error, I already tried using the @foreach(optional($filter->filter_values) as $filter_value) Based on some of the solutions I can accross, with this the error is fixed and the thing now is nothing returns in the array even when they are not empty. I must have put the "optional" in the wrong place as when I remove it the error is thrown again

                 @foreach($filters as $filter)

                                                <div class="input__field">

                                                    <div class="select">

                                                        <label for="{{ $filter->alias }}">{{ $filter->name }}:</label>

                                                        <select class="js_pretty_select js__select__{{ $filter->alias }} select__{{ $filter->alias }}" id="{{ $filter->alias }}" name="filters[{{ $filter->id }}]">

                                                            <option value="0">--- Select value ---</option>

                                                            @foreach($filter->filter_values as $filter_value)

                                                                <option value="{{ $filter_value->id }}" @if(in_array($filter_value->id, $filterValuesIds) || old('filters')[$filter->id] ?? 0 === $filter_value->id) selected @endif>{{ optional($filter_v

                                                            @endforeach

                                                        </select>

                                                    </div>

                                                </div>

                                            @endforeach

Please any help will great, thanks

0 likes
7 replies
Sinnbeck's avatar

The error most likely comes from this line

old('filters')[$filter->id] ===
//try giving it a fallback
old('filters')[$filter->id] ?? 0 ===
DavP2021's avatar

@Sinnbeck Thanks for your fast response, Your are spot on, the error is from there. I made the changes but still the same error. I have update the code with the changes please have a look.

DavP2021's avatar

@Sinnbeck This is line where the error points

<option value="{{ $filter_value->id }}" @if(in_array($filter_value->id, $filterValuesIds) || old('filters')[$filter->id] ?? 0 === $filter_value->id) selected @endif>{{ $filter_value->name  }}</option>

If for example I remove the if condition, there is no error with the code below

<option value="{{ $filter_value->id }}">{{ $filter_value->name  }}</option>
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@DavP2021 This the same error? Maybe try wrapping it. It works for me in tinker

 (old('filters')[$filter->id] ?? 0)
DavP2021's avatar

@Sinnbeck Thanks a lot it works. Here the solution for anyone who may have similar issue

<option value="{{ $filter_value->id }}" @if(in_array($filter_value->id, $filterValuesIds) || (old('filters')[$filter->id] ?? 0) == $filter_value->id) selected @endif>{{ $filter_value->name }}</option>

Please or to participate in this conversation.