0

HTML :

<tr v-for="role in menu.roles" v-bind:key="role.id">
    <td>{{role.name}}</td>
    <td v-for="action in actions" :key="action.id">
        <input type="checkbox" name="actions_id" :value="action.id" :label="action.name" v-model="form.actions.actions_id.actions_id">
   </td>
</tr> 

I want that if I select 3 it saves into the database.

 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

Controller :

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

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

        $form->update();

        return response()->json();
    }

For the prepopulate currently i have this in my repository

public function getAllActions()
    {
        $allactions = Action::first();

        return $allactions;
    }

and this

public function editRole($id)
    {
        $menurole = $this->accesscontrolRepository->getMenuRoles($id);

        $allactions = $this->accesscontrolRepository->getAllActions();


        $actionHelper = actions();

        return response()->json([
            'menurole' =>$menurole,
            'actions' => $actionHelper,
            'allactions' => $allactions,
        ]);
    }

in my controller but the problem is that if i click update after making edit to the checkbox the new updated checkbox is not checked which is because of the first() in the repository

NOTE: 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

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

  • In `updateMenuRoles()` you always call `create`. Do you need a default value for the inputs? An unchecked checkbox does not have a value. – Peter Krebs Feb 02 '22 at 10:25
  • Does this answer your question? [Trying to set default checkbox value if not checked](https://stackoverflow.com/questions/17688968/trying-to-set-default-checkbox-value-if-not-checked) – Peter Krebs Feb 02 '22 at 10:25
  • @PeterKrebs Yeah that is in PHP but how do i do this with VueJs? – Henry Oladeji Feb 02 '22 at 10:52
  • That's not PHP specific. That's how HTML checkboxes work. Just make the HTML the same as in the question - a hidden field with the same name before the checkbox. – Peter Krebs Feb 02 '22 at 11:07

0 Answers0