Thursday, March 30, 2017

Laravel set checkboxes checked based on database value

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" "/>
        <label for="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?



via raqulka

Advertisement