Пытаюсь сделать обновление через 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);
То, что закомментировано работает, но это уж совсем в лоб. Почему может не срабатывать updateOrCreate?
Данные приходят в таком формате.
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}
и т.д.
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();
});
//City.php
public function businessHours()
{
return $this->hasMany(
BusinessHour::class
);
}
//BusinessHour.php
public function city()
{
return $this->belongsTo(City::class);
}
Создавал через сидер -
$bHours = Config::get('businessHours');
$city->businessHours()->createMany($bHours);
Все прописалось как нужно. Но теперь не хочет обновлять.
за названия переменных не пинайте...
п.с.
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
что-то мне подсказывает, что это явно плохо...