How to show old data of dynamic checkbox in Laravel?

1st WAY

First create variable

$hob = old('hobby');

and than check is array or not

echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL;

and then apply it.

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" <?php echo is_array($hob) ? (in_array(1, $hob)?'checked':NULL) : NULL; ?>> football

2nd WAY

OR you can do that but in this case you will not create variable

@if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif

and then apply it.

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1,old('hobby'))) checked @endif > football


These all appear to work, a cleaner solution that I adopt takes the form of the following...

<input type="checkbox" name="departments[]" value="{{ $item->id }}" {{ in_array($item->id, old('departments', [])) ? 'checked' : '' }}>

Where $item is the object while iterating over a collection,

Rather than explicitly checking if we have an array, we can defer the default value of the old value as being an array in the instance the form has yet to be submitted or there were no options checked on the original form request.

Referring back to OPs original query, it could be structured similar to the below

<input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ in_array(1, old('hobby', [])) ? 'checked' : '' }}>

You can use

<input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(in_array(3, old('hobby'))) checked @endif>

Or you could just switch to use Laravel Collective https://laravelcollective.com/docs/5.3/html#checkboxes-and-radio-buttons

And have code like this instead:

{!! Form::checkbox('name', 'value', true) !!}

Update for Laravel 9.x as Yusuf T suggested (refer this document) :

<input type="checkbox" name="active" value="active" @checked(old('active', $someVariableOrValue )) />

For older versions this will work:

<div class="form-check">
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="1" @if(is_array(old('hobby')) && in_array(1, old('hobby'))) checked @endif> football
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="2" @if(is_array(old('hobby')) && in_array(2, old('hobby'))) checked @endif> basketball
    </label>
    <label class="form-check-inline">
        <input class="form-check-input" type="checkbox" name="hobby[]" value="3" @if(is_array(old('hobby')) && in_array(3, old('hobby'))) checked @endif> swimming
    </label>
</div>

Also as brokekidweb suggested :

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) and in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>

OR

<label class="form-check-inline">
    <input class="form-check-input" type="checkbox" name="hobby[]" value="1" {{ (is_array(old('hobby')) && in_array(1, old('hobby'))) ? ' checked' : '' }}> football
</label>

Tags:

Php

Laravel