MrBelongsTo
MrBelongsTo

Reputation: 43

using old on checkbox

I have a checklist which was generate in a @foreach. I have one simple question. How can I use {{ old() }} in this example. My foreach:

@foreach($c->tags as $tag)
    <li><label><input class="form-check-input" type="checkbox" name="tags[]" value="{{ $tag->id }}"> {{ $tag->name }}</label></li>
@endforeach

I want to know if my checkbox was check (after submitting, if I'll have errors). In input text is simple {{ old('name') }}

Upvotes: 1

Views: 3407

Answers (2)

JanBoehmer
JanBoehmer

Reputation: 555

For one:

<input type="checkbox" name="example" {{ !old('example') ?: 'checked' }}>

For many:

<input type="checkbox" name="example[]" value="val" @if (is_array(old('example')) && in_array('val', old('example'))) checked @endif>

Upvotes: 0

Joel Hinz
Joel Hinz

Reputation: 25404

Make a check to see if it exists and check it if it does. In its basic form:

<input type="checkbox" name="example" @if(old('example')) checked @endif>

Upvotes: 2

Related Questions