Reputation: 1028
I am trying to set checkboxes checked based on database value. One user can have have multiple checkbox values.
Checkboxes are generated from database table called child_age_groups
:
@foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" name="age_group[]" id="age_group{{$age->id}}" "/>
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
@endforeach
So in my controller I get all the options that user has like this
$nanny_babysitting_ages = Nanny_babysitting_ages::where('user_id', user()->id)->get();
My nanny_babysitting_ages table is this
user_id | ages
and my child_age_groups table where I populate the checkboxes are this:
id | age_group
How can I set the checkboxes selcted based on the values from database?
Upvotes: 2
Views: 7683
Reputation: 23
you can use this "@checked(true)" like :- <input class="custom-control-input army" type="checkbox" id="customCheckbox1" @checked($emp_plus->army != 0)>
Upvotes: 0
Reputation: 1028
This is how I resolved it, I added foreach and if statement in the input to check if it the same value as from database:
<div class="form-group" id="ageCheckboxes">
@foreach ($child_age_groups as $age)
<div class="checkbox checkbox-info checkbox-inline">
<input class="age_group_checkbox" type="checkbox" value="{{$age->id}}" name="age_group[]" id="age_group{{$age->id}}" @foreach ($nanny_babysitting_ages as $ages) @if($age->id == $ages->ages ) checked @endif @endforeach />
<label for="age_group{{$age->id}}">{{$age->age_group}}</label>
</div>
@endforeach
</div>
Upvotes: 4