vendredi 2 mars 2018

How to Update multiple row of a table in laravel

Helper,

I wanna update multiple addresses for a particular user_id, with out changing its existing order. Also I'm using relation here.

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

$user = User::with('addresses')->find($id);

$j = $user['addresses'][0]['id'];

foreach($request->address_id as $key => $i){
    $i++;
}

foreach($request->address1 as $key => $v){
        if($j<$i){
            $user->addresses()->whereUserId($data['id'])->update([
                'address1' => $v,
                'address2' => $request->address2[$key],
                'state' => $request->state[$key],
                'country' => $request->country[$key]
            ]);
        }
    $j++;}
}

I'm getting input somewhat like this,

{"_method":"PATCH","address_id":["92","93"],"address1":["aaa","xxx"],"address2":["bbb","yyy"],"state":["ccc","zzz"],"country":["India","India"]}

When I do my update the last input, which means, xxx,yyy,zzz,India is getting update for both rows92,93



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire