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

monstajamss's avatar

submit data as an json array

I have my form as this with check box

<input type="checkbox" value="1" name="actions_id">
<input type="checkbox" value="2" name="actions_id">
<input type="checkbox" value="3" name="actions_id">

I want that if i select the 3 it saves into the database in json i have this in my repository

 public function updateMenuRoles(Request $request, $id)
    {

        $form = Action::create(array($id));

        return $form;
    }

Note: The form is not a create form, it is an update form so when you click edit and you saves the data but if you make changes to the checkbox by checking maybe one or two and then you click submit it generates another data in the database.

What i want is for it to update the previous one generated instead of always creating new record

I have this in my controller

public function updateRole(Request $request, $id)
    {
        
        $form = $this->accesscontrolRepository->updateMenuRoles($request, $id);

        $form->actions_id = $request->actions_id;

        $form->update();

        return response()->json();
    }
0 likes
5 replies
Nakov's avatar

First of all since it is a checkbox, you should set the name to name="actions_id[]" an array as, so multiple items can be selected. Then since it is an update, you should pre-select what is already checked in the database. So for that you should add:

@if($item->role === 1) checked @endif

to each of your inputs. Of course change the code above with what you have.

monstajamss's avatar

@Nakov Is there no way this can be done in the controller or repository instead?

monstajamss's avatar

@Nakov The thing is about checking a box and creating a record of the checked boxes and if i uncheck and check the boxes, it should update that same record and not create a new one if the record already exists.

The code in which i posted above keeps creating new record for every check/uncheck boxes

monstajamss's avatar

For example i have no record in the table so i click 3 checkboxes which creates a record right? so when i go back to uncheck those check boxes a new record is created which i want to avoid because fetching the record would be difficult if it keeps creating new record

Please or to participate in this conversation.