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

johnef_sh's avatar

Getting a checkbox checked

I have group of checkbox which I add to database as an array [1,2,3] everything going OK when I adding it but what about retrieve it from the database to edit.

here is my controller

public function edit($id)
    {
        $temp = Template::query()->findOrFail($id);
        $extra = TempExtraService::all();
        return view('admin.templates.tempDetails.edit', compact('temp', 'extra'));
    }

and here is my view

<div class="form-group">
                {!! Form::label('extra_services', 'Add extra services') !!}
                <div class="form-group">
                    @foreach($extra as $ext)
                        <div class="checkbox">
                            <label>
                                {!! Form::hidden('extra_services[]', 0) !!}
                                {!! Form::checkbox('extra_services[]', $ext->id, $temp->extra_services, ['class' => 'checkbox'])  !!}
                                {!! $ext->title !!}
                            </label>
                        </div>
                    @endforeach
                </div>
            </div> 

I get all the checkbox s checked even if they are not even review the table after editing got something like this 0,1,0,2,0

0 likes
6 replies
jlrdw's avatar

You have to "test" if it's true or false, 0 or 1. If true or 1 check it in code.

johnef_sh's avatar

@jlrdw I don't think this will work, because the out coming is an array I think it has something to do with if inArray but still working on it to get it the right way

johnef_sh's avatar

I changed my view to be

<div class="form-group">
                {!! Form::label('extra_services', 'Add extra services') !!}
                <div class="form-group">
                    <?php var_dump (array($temp->extra_services))?>
                    @foreach($extra as $ext)
                        <div class="checkbox">
                            <label>
                                {!! Form::hidden('extra_services', 0) !!}
                                <input type="checkbox" name="extra_services[]" value="{!! $ext->id !!}"
                                       {!! in_array($ext->id, array($temp->extra_services)) ? 'checked' : '' !!} >
                                {!! $ext->title !!}
                            </label>
                        </div>
                    @endforeach
                </div>
            </div>

but now the first checkbox only is checked any idea

jlucia's avatar

I would suggest storing the array in the database as a JSON string. So on the store method you could use json_encode($request->extra_services) and then when you fetch your records you could then decode it in your foreach loop.

<div class="form-group">
    {!! Form::label('extra_services', 'Add extra services') !!}
    <div class="form-group">
        @foreach($extra as $ext)
            <div class="checkbox">
                <label>
                    {!! Form::hidden('extra_services', 0) !!}
                    <input 
                        type="checkbox" 
                        name="extra_services[]" 
                        value="{!! $ext->id !!}"
                        {{ in_array($ext->id, json_decode($temp->extra_services)) ? 'checked' : '' }}>
                    {!! $ext->title !!}
                </label>
            </div>
        @endforeach
    </div>
</div>
johnef_sh's avatar

@jlucia thanks for your suggesting here is how my controller looks like

on the store method

$temp->extra_services = implode($request->extra_services, ',');

so you mean it should be something like this

$temp->extra_services = json_encode($request->extra_services)

do I got you right

jlucia's avatar

Yes, it should store the id values nicely with no additional work. Then the decode will turn it into an array for your in_array function.

Please or to participate in this conversation.