mardi 19 mars 2019

laravel update multiple request arrays

I'm really struggling with this update method, any help will be heartfully appreciated.

I'm send multiple car number plates, that is assocaited with the resident, i can successfully store and retrive it, now i want to update it.

when I return or dd on $request->cars, it gives me this array.

array:2 [
0 => array:5 [
"id" => 10
"user_id" => 33
"car_number_plate" => "12f3456"
"created_at" => null
"updated_at" => null
]

1 => array:5 [
"id" => 11
"user_id" => 33
"car_number_plate" => "abc -123"
"created_at" => null
"updated_at" => null
]   
]

Here I find the accociated id, that needs to be updated

$userId = ResidentCar::where('user_id', $resident->id)->pluck('id')
->toArray();

it return this array.

array:2 [
0 => 10
1 => 11
]

Here i loop through the array and trying to update it.

$input = $request-cars;

foreach ($input as $row)
{
$cars[] = [
    'user_id' => $resident->id,
    'car_number_plate' => $row['car_number_plate'],
];
}

ResidentCar::whereIn('id', $userId)->update(array($cars));

it gives this error of, Array to string conversion.

and if do it with

ResidentCar::whereIn('id', $userId)->update($cars);

it gives this error.

Column not found: 1054 Unknown column '0' in 'field list' (SQL: update `resident_cars` set `0` = 33, 

Can anyone here please help on how to solve it, or any better approach thin this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire