pmiranda
pmiranda

Reputation: 8420

Laravel 5.1, update multiple values from checked checkbox

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?

Upvotes: 0

Views: 1733

Answers (2)

pmiranda
pmiranda

Reputation: 8420

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();
}

Upvotes: 1

Tarasovych
Tarasovych

Reputation: 2398

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
}

Upvotes: 1

Related Questions