Forum

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

johnef
johnef
  • 1 month ago

It looks like the $extra is generating multiple Extra Service rows, however the same value from the $temp/Template is being passed though to each as your doing a "->findOrFail()". I'm not sure how the data is stored here, but I'd check where you are pulling in the boolean for each Extra Service and get that data available.