Answer the question
In order to leave comments, you need to log in
What could be the essence of the error when updating the data?
I'm trying to update via hasMany.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':00:00 `50` = ? 18:00:00 `51` = ? 18:00:00 `52` = ? 18:00:00 `53` = ? 18:00:0...' at line 1 (SQL: select * from `business_hours` where `business_hours`.`city_id` = 8 and `business_hours`.`city_id` is not null and (18:00:00 `50` = monday 18:00:00 `51` = tuesday 18:00:00 `52` = wednesday 18:00:00 `53` = thursday 18:00:00 `54` = friday 18:00:00 `55` = saturday 18:00:00 `56` = sunday) limit 1)
$city = City::find($id);
$businessH = $request->data['business_hours'];
// foreach ($businessH as $day) {
// $hours = BusinessHour::find($day['id']);
// $hours->update($day);
// }
$res = $city->businessHours()->updateOrCreate($businessH);
0: {id: 29, day: "monday", open: "10:00:00", close: "18:00:00", extra: "", city_id: 5}
1: {id: 30, day: "tuesday", open: "10:00:00", close: "18:00:00", extra: "", city_id: 5}
etc.Schema::create('business_hours', function (Blueprint $table) {
$table->id();
$table->string('day');
$table->time('open')->nullable();
$table->time('close')->nullable();
$table->string('extra')->nullable();
$table->foreignID('city_id')->constrained();
});
public function businessHours()
{
return $this->hasMany(
BusinessHour::class
);
}
public function city()
{
return $this->belongsTo(City::class);
}
update `business_hours` set `extra` = null where `id` = 7 2.34ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 7 limit 1 1.42ms 6s ago
update `business_hours` set `extra` = null where `id` = 6 2.04ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 6 limit 1 1.51ms 6s ago
update `business_hours` set `extra` = null where `id` = 5 3.33ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 5 limit 1 1.44ms 6s ago
update `business_hours` set `extra` = null where `id` = 4 2.40ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 4 limit 1 1.39ms 6s ago
update `business_hours` set `extra` = null where `id` = 3 2.47ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 3 limit 1 1.18ms 6s ago
update `business_hours` set `extra` = null where `id` = 2 1.92ms 6s ago
select * from `business_hours` where `business_hours`.`id` = 2 limit 1 1.01ms 6s ago
update `business_hours` set `extra` = null where `id` = 1 2.21ms 6s ago
Answer the question
In order to leave comments, you need to log in
updateOrCreate
is designed to update a single record, not an array.
And besides, you are also using it incorrectly:
updateOrCreate(<атрибуты поиска>, <обновляемые значения>)
If you pass all businessHours when updating, then:
$city->businessHours()->delete();
$city->businessHours()->createMany(businessH);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question