如何在Laravel中更新表的多行?

时间:2018-03-02 10:13:43

标签: laravel laravel-5 laravel-5.5

帮助器,

我想更新特定user_id的多个地址,而不更改现有订单。我也在这里使用关系。

public function update(Request $request, $id){
$data['id'] = $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++;}
}

我的输入有点像这样,

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

当我更新最后一个输入时,这意味着xxx,yyy,zzz,India正在更新两行92,93

0 个答案:

没有答案