Answer the question
In order to leave comments, you need to log in
How to set the value of a variable when the checkbox is deselected?
Kind everyone. I have a form, it has checkboxes, when the checkbox is checked, then the value for the variable opposite to which it is made gets into the array.
How can I put an empty string into the array when the checkbox is deselected? I need it to sync tables in db using sync() method.
Checkbox code:
<input class="form-check-input" name="category[{{$category->id}}][{{$role->id}}]" value="{{$role->id}}" type="checkbox"
@if(in_array($role->id, $category->roles()->pluck('id')->all())) checked @endif>
Answer the question
In order to leave comments, you need to log in
To do this, a hidden input with the same name and the desired value is added to the markup before the checkbox.
If the checkbox is checked, then when the form is submitted, it will overwrite the hidden input, and if not checked, the value from the hidden one will be sent.
<input type="hidden" name="category[42][11]" value="0" class="form-check-input" />
<input type="checkbox" name="category[42][11]" value="1" class="form-check-input" />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question