I honestly have no clue what your question is. Can you give us some more context?
Oct 4, 2020
10
Level 3
Checkbox array - check if in DB - blade
Hello guys, I have something likt that:
@foreach($artbook->category as $value)
<label><input type="checkbox" name="category[]" value="1" {{ $value==='1' ? 'checked' : '' }}> 1</label>
<label><input type="checkbox" name="category[]" value="2" {{ $value==='2' ? 'checked' : '' }}> 2</label>
<label><input type="checkbox" name="category[]" value="3" {{ $value==='3' ? 'checked' : '' }}> 3</label>
<label><input type="checkbox" name="category[]" value="4" {{ $value==='4' ? 'checked' : '' }}>4</label>
@endforeach
it works, but when I have more than 1 value checket it multiple records. For example: checked in DB values 1 and 2, in result it gives in blade:
1 - checked, 2 - checked, 3 - unchecked, 4 - unchecked 1 - checked, 2 - checked, 3 - unchecked, 4 - unchecked
How can I modify this code to get only one row with options? At the moment, it multiplies records by the number of items in the array, but I need to check checkbox if in DB. Thanks
Level 75
<label><input type="checkbox" name="category[]" value="1" {{ in_array(1, $artbook->category) ? 'checked' : '' }}> 1</label>
<label><input type="checkbox" name="category[]" value="2" {{{ in_array(2, $artbook->category) ? 'checked' : '' }}> 2</label>
<label><input type="checkbox" name="category[]" value="3" {{ in_array(3, $artbook->category) ? 'checked' : '' }}> 3</label>
<label><input type="checkbox" name="category[]" value="4" {{ in_array(4, $artbook->category) ? 'checked' : '' }}>4</label>
Please or to participate in this conversation.