mardi 6 mars 2018

Update multiple columns in laravel

Helper,

I'm having two table as users and addresses. I have saved multiple addresses for a user. Now I wanna update one of the users address. Is there any single query for update. I'm getting every input as array.

*users table*
id | name
1  | Mr.X


*addresses table*
id | user_id | city | state |country
1  |   1     | XXX  | YYY   | ZZZ
2  |   1     | AAA  | BBB   | CCC

I have tried below code, but it do change in all columns.

public function update(Request $request, $id){
   foreach($request->city as $key => $v) {
        $user->addresses()->where('user_id', '$id')->update([
            'city' => $v,
            'state' => $request->state[$key],
            'country' => $request->country[$key]
        ]);
    }
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tmGm9i
via IFTTT

Aucun commentaire:

Enregistrer un commentaire