简体   繁体   中英

"SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2018' for column 'created_at' at row 1 (SQL: insert into `news`

So, I am currently encountering with this error, the problem is when I want to create a new record this error occurs but when I update nothing happens, my DB is MySql and it is set to utf8, I have one local and one currently deployed, the one that I am deploying has utf8 but the local one has utf8mb4 I've looked at other questions similar to mine but I don't think they have the same solution.

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2018' for column 'created_at' at row 1 (SQL: insert into news ( title , description , date , pictures , categroy_id , updated_at , created_at ) values (zxczx,

xzxczxc

, 09/28/2018, ["3_1538054794.jpg"], 6, 2018-09-27 13:26:34, 2018-09-27 13:26:34))

and that's how I save it

    $new = new News;

    $new->title = $input['title'];
    $new->description = $input['description'];
    $new->date = $input['date'];
    $new->pictures = json_encode($pics);
    $new->categroy_id = $input['category'];
    $new->save();

You use Carbon to parse the date it will convert it to date format which is used by MySql YYYY-m-dd

$new = new News;
$new->title = $input['title'];
$new->description = $input['description'];
$new->created_at = Carbon::now();
$new->pictures = json_encode($pics);
$new->categroy_id = $input['category'];
$new->save();

Try like this :

$new = new News;
$new->title = $input['title'];
$new->description = $input['description'];
$new->pictures = json_encode($pics);
$new->date = $input['date'];
$new->categroy_id = $input['category'];
$new->created_at = new \DateTime();
$new->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM