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' : '' }}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.