我使用 Laravel 和 SQLite 创建了一个 Web 应用程序,一切都很顺利。现在,我想使用 MySQL 作为数据库,但现在我无法将数据添加到我的表中。
这是我迁移的字段:
$table->dateTime('last_updated')->nullable();
这就是我使用 laravel 填充该字段的方式:
$client->last_updated = time();
但它给了我这样的错误:
Illuminate \ Database \ QueryException
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1402140378' for column 'last_updated' at row 1 (SQL: update `clients` set `last_updated` = 1402140378 where `id` = 1)
任何帮助将不胜感激。
最佳答案
$table->dateTime
创建 DATETIME
数据库中需要值为 YYYY-MM-DD HH:ii:ss
的列。您要插入的是 unix 时间戳。
您可以使用Carbon
(需要在文件顶部使用 Carbon\Carbon
),
$client->last_updated = Carbon::now()
$client->last_updated = date('Y-m-d H:i:s')
或 PHP 的 DateTime
类将当前日期时间插入数据库..
关于Laravel 和 MySQL 在 DateTime 方面表现不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24096730/