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

Nikki's avatar
Level 1

Checkbox not displaying a tick

In my page if I have mutliple checkboxes and what can be done is the user can check one or more of the checkboxes. The problem I'm having is that after the user saves and goes back to the page the checkboxes that was checked isn't checked.

What happens on the page is that the user will select the appropriate boxes and it gets saved as an array in the database.

The column in the table is called dci and it is in the product table

My controller

public function edit($id)
    {
        $products = Product::find($id);

        $category_options = ProductsCat::pluck('title', 'id');

        $category_item_id = ProductsCat::find($id);

        $selected_options = $products->productscat()
                      ->select('productscat.id') // you need to specify SELECT clause since there are joins
                      ->pluck('id')
                      ->toArray();



        if(is_null($products)){
            return redirect()->route('products.edit');
        }

        return view('products::admin.edit', compact('products', 'category_options', 'category_item_id', 'selected_options'));
    }

my edit.blade.php

{!! Form::model($products, array('method' => 'PATCH', 'route' => array('product.update', $products->id), 'class' => 'add-form')) !!}
                <div class="form">

                    <div class="form_input">
                        <div>
                            {!! Form::label('productcat_id', 'Category') !!}
                        </div>
                        <div>
                            {!! Form::select('productcat_id', $category_options, $selected_options, array("class" => "form-control")) !!}
                        </div>
                    </div>

                    <input type="checkbox" id="Domestic" value="Domestic" name="dci[]" {!! $products->dci === 'Domestic' ? 'checked' : '' !!}>
                    <label for="Domestic">Domestic</label>

                    <input type="checkbox" id="Commerical" value="Commerical" name="dci[]" {!! $products->dci === 'Commerical' ? 'checked' : '' !!}>
                    <label for="Commerical">Commerical</label>

                    <input type="checkbox" id="Industrial" value="Industrial" name="dci[]" {!! $products->dci === 'Industrial' ? 'checked' : '' !!}>
                    <label for="Industrial">About</label>


                    <div class="form_input">
                        <div>
                            {!! Form::label('title', 'Title') !!}
                        </div>
                        <div>
                            {!! Form::text('title', $products->title, array('id' => 'title', 'class' => 'form-control')) !!}
                        </div>
                    </div>
        </div>
    {!! Form::close() !!}

If I've forgotten to add other data please let me know

0 likes
6 replies
sutherland's avatar

Instead of just comparing the value like you're doing here:

{!! $products->dci === 'Domestic' ? 'checked' : '' !!}

you need to check if the value is in the array:

{{ in_array('Domestic', $products->dci) ? 'checked' : '' }}
Nikki's avatar
Level 1

I'm getting this error

in_array() expects parameter 2 to be array, string given

but in my database I have this

["Domestic","Commerical"]
Nikki's avatar
Level 1

How do I set the table that if there is nothing checked then an empty array gets put. Because at the moment I have it set to NULL and when I try to get to that page I get this error

in_array() expects parameter 2 to be array, null given

which I understand because in the table it is null and not an array

topvillas's avatar

Use a ternary operator before you pass the array to the view.

$products->dci = $products->dci ?: [];
sutherland's avatar

Then define an accessor like this on your model:

    public function getDciAttribute($value)
    {
        if (is_null($value)) {
                return [];
        }

        return $value;
    }

Please or to participate in this conversation.