0

In Laravel 5.1, I need to update multiple values from checked checkbox.

I can edit some registries from a table by clicking the edit button for each registry, and that button send me to the edit view

(This is de edit view for a single registry) enter image description here

With the url: http://myapp/someroute/2246/edit where 2246 is some id.

Inside that edit I can update 4 fields. One of those fields is called "my state" and can have the values 1, 2 or 3.

Now, I have to make a multi select edit feature, where I can check every row of the table that I need to update simultaneously (each have the name=someid) and then click some button called "Validate", and update for evey row only 1 field, the my state field, and the new value will be always 1 (in the picture the values are string but thats only for the view).

enter image description here

The question is: how can I call the method update for every id that I'm selecting in the view? every input checkbox has it's own name which is the id of the registry that I will update.

The update method just validate some values from the view and then call some myeditmethod, but in this case I will jump the update and go directly to myedit which is someting like:

public function myedit(Request $request, $id) {
    $obj                  = Self::findOrFail($id);
    $obj->fk_id_comuna    = $req['fk_id_comuna'];
    $obj->fk_id_user      = $usuario_id;
    $obj->date            = \Carbon\Carbon::now();
    $obj->fk_id_my_state  = $estado; //THIS IS THE ONLY FIELD THAT I WILL EDIT, ALWAYS WITH THE SAME VALUE `1`
    $obj->save();

I was trying the make a form for that Validate button but I don't know how to handle multiple id in one call on the edit method.

<form action="{!! route('myroute.update', ['id' => [HERE, HOW CAN I PASS MULTIPLE ID FROM THE CHECKED CHECKBOX] ]) !!}" method="POST">
    <input type="submit" class="btn btn-primary pull-right" value="Validar" />
</form>

I was thinking on a javascript function which collect in a array every checked checkbox name and call the myedit method directly, without the formof the view, could be?

2 Answers 2

1

About passing multiple values as one Request value.
Assume you have form like this:

<form method="post">
    <input type="checkbox" name="options[]" value="foo"/>foo<br/>
    <input type="checkbox" name="options[]" value="bar"/>bar<br/>
    <input type="checkbox" name="options[]" value="buz"/>buz<br/>
    <input type="submit" value="Submit" />
</form>

Your request('options') would be an array: ["foo", "bar", "buz"].
Than you can iterate over options using foreach.

Inside your update method you can go with:

foreach ($option as request('options')) {
    //put your previous code here, so it'd be applied for every option
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, and nice, it's a good starting point. I will work based on this, and I'll pick it as the correct one if this helps me to make this works.
I did it with javascript, I will pick your answer as the correct one because that foreach gave me the whole idea. I'll put my own answer of what I did.
1

In JS I did this:

var optionsChecked = [];
    $('.options:checkbox:checked').each( function(){
    optionsChecked .push($(this).val());
});

Then in ajax:

$.ajax({
    type: 'POST',
    data: {'id': optionsChecked },
    etc

Then in PHP:

$all = $request->input('id');
foreach ($all as $id){
    //whole obj->* = *;
    $obj->save();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.