Reputation: 17
I have two tables with left join and want to update by area_id. my update code is:
$area = Area::find($id);
$city_id = DB::table('areas')->where("areas.id", $id)->select("areas.city_id")->join("locations", "areas.city_id", "=", "locations.id")
->get();
if ($area) {
$area->area_name = $request->input('areaName');
$area->city_id = $city_id;
$area->update();
return response()->json([
'status' => 200,
'message' => $area['areaName'] . ' Updated successfully',
]);
} else {
return response()->json([
'status' => 400,
'message' => "Area Not found",
]);
}
when I perform this I got an error which is below:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[{\"city_id\":1}]' for column `laravel_drywashbd`.`areas`.`city_id` at row 1 (SQL: update `areas` set `area_name` = Wari25, `city_id` = [{\"city_id\":1}] where `id` = 3)"
How can I solve this? Please help me.
Upvotes: 1
Views: 1374
Reputation: 6269
It looks that your city_id
created as datetime
from your migration file but it should be unsignedBigInteger
instead
another thing this query you should use first
instead of get
$city_id = DB::table('areas')->where("areas.id", $id)->select("areas.city_id")->join("locations", "areas.city_id", "=", "locations.id")->first();
Upvotes: 1