D
D
Dmitry2020-12-06 05:09:04
Laravel
Dmitry, 2020-12-06 05:09:04

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)

strange data. I put the Telescope, but I can’t catch the request itself, which is being formed - I see only this passage in the errors.
$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);


What is commented out works, but it's completely in the forehead. Why might updateOrCreate not work?
The data comes in this format.
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();
        });


//City.php
public function businessHours()
    {
        return $this->hasMany(
            BusinessHour::class
        );
    }


//BusinessHour.php

public function city()
    {
        return $this->belongsTo(City::class);
    }


Created via seeder -
$bHours = Config::get('businessHours');
$city->businessHours()->createMany($bHours);
Everything was written as it should. But now he does not want to update.
do not kick for the names of variables ...

p.s.
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


something tells me that this is obviously bad ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-12-10
@dlnsk

updateOrCreateis designed to update a single record, not an array.
And besides, you are also using it incorrectly:

updateOrCreate(<атрибуты поиска>, <обновляемые значения>)

A
Alexander Sqweez, 2020-12-11
@Sqweez

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 question

Ask a Question

731 491 924 answers to any question