Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ziben69's avatar

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

0 likes
10 replies
bobbybouwmann's avatar

I honestly have no clue what your question is. Can you give us some more context?

2 likes
MichalOravec's avatar
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>
ziben69's avatar

Hmm. It's hard to describe ;p

I have a column in my table called category. it holds an array. When I add a record to the database with 3 categories selected in the view, I get an array. In the edit view, I am trying to check which category is selected. And here I have a problem. The above code shows me the selected items but multiplies them as many times as I have selected categories when creating. So by selecting 3 categories, I get 3 rows of the same categories

Snapey's avatar

You need to have a loop of ALL possible values, not a loop of the ones previously selected.

what does $artbook->category look like?

ziben69's avatar

its a decoded jqon:

public function getCategoryAttribute($value)
    {
        return $this->attributes['category'] = json_decode($value);
    }
ziben69's avatar

in my Model is error: json_decode() expects parameter 1 to be string, array given

public function getCategoryAttribute($value)
    {
        return $this->attributes['category'] = json_decode($value);
    }

when I try @michaloravec solution

Snapey's avatar

what does this even do?

public function getCategoryAttribute($value)
    {
        return $this->attributes['category'] = json_decode($value);
    }

Its not valid.

If category is stored as a json string, create a casts entry for it so that Eloquent knows to convert it to an array.

or;

    public function getCategoryAttribute()
    {
        return json_decode($this->attributes['category'])
    }
MichalOravec's avatar

It seems to me that you mix accessor with mutator together.

ziben69's avatar

Thanks for lesson, now it works :)

Please or to participate in this conversation.