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

freel's avatar

Checkbox array - old('value')

Head is not working... how to return old value after validation to this? I already tried so many different ways...

<ul class="optional-features-list">
    @foreach ($features as $feature)
        @if ($feature->category_name == 'Interior Features')
            <li class="checkbox"><label><input id="features" name="feature[{{$feature->id}}]" type="checkbox">{{$feature->feature_name}}</label></li>
        @endif 
    @endforeach
</ul>

here is how it looks data:

array:11 [▼
  "_token" => "HzN2Niy33fjSWtPF4kUY003kAGScvvzCUCABV5Ia"
  "plate_number" => "any"
  "feature" => array:3 [▼
    1 => "1"
    4 => "4"
    7 => "7"
  ]
0 likes
14 replies
willvincent's avatar

You can't use if/endif inline like that.. But I think this ought to work:

<input id="features" 
       name="feature[{{$feature->id}}]" 
       type="checkbox" 
       value="1"  
       {!! old("feature[$feature->id]") ? 'checked="checked"' : '' !!}>
willvincent's avatar

Hmm.. I've tried and it didn't work, maybe blade supports it now. Or do you have it on separate lines?

freel's avatar

Hello guys. Non of your suggestions worked... any other ideas...

freel's avatar

maybe by form not returning old('key') value? how to debug? with dd(old('key')) geting null ....

freel's avatar

Thx first example working perfect! thank you.

1 like
freel's avatar

quick question on the end. now I have values: "feature_1" => "1" "feature_4" => "4" "feature_5" => "5" "feature_8" => "8" "feature_39" => "39" "feature_42" => "42" "feature_65" => "65" "feature_68" => "68"

how you checking how many max values you have ? I am not sure is it good idea to run for when I don't know how many values we have...

cklmercer's avatar

Give this a whirl and let us know.

<ul class="optional-features-list">
    @foreach ($features as $feature)
        @if ($feature->category_name == 'Interior Features')
        @if(isset(old('feature.' . $feature->id'))
                <li class="checkbox">
                    <label><input id="features" name="feature[{{$feature->id}}]" type="checkbox" checked>{{$feature->feature_name}}</label>
                </li>
            @else
                <li class="checkbox">
                    <label><input id="features" name="feature[{{$feature->id}}]" type="checkbox">{{$feature->feature_name}}</label>
                </li>
            @endif
        @endif 
    @endforeach
</ul>
frezno's avatar

@willvincent

Hmm.. I've tried and it didn't work, maybe blade supports it now. Or do you have it on separate lines?

It's all in one line. But yours looks nicer and probably is more 'future-consistent' than mine ;)

sorcjc's avatar

This is an old post, but here is another example (using the same name for all the checkboxes, with different values):

@foreach ($categories as $category)
    <div class="col-md-4 col-sm-6">
        <input type="checkbox" name="categories[]" value="{{ $category->id }}" {{ ( is_array(old('categories')) && in_array($category->id, old('categories')) ) ? 'checked ' : '' }}/> {{ $category->name }}
    </div>
@endforeach
6 likes
Go3shom's avatar

As the new directive @checked mentiond In Laravel 9.x; this worked for me too

<input 
    type="checkbox" 
    name="tags[]"
    @checked(in_array($tag->id, old('tags', [])))
    value="{{ $tag->id }}"
>
1 like

Please or to participate in this conversation.