【发布时间】:2020-05-02 14:10:27
【问题描述】:
我想直接从迁移中将日期格式从 1990-01-30 更改为 30/01/1990。当我尝试从工厂播种迁移时出现以下错误。
日期时间格式无效:1292 日期值不正确:“30/01/1990” 第 1 行的“dob”列
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('fname');
$table->string('lname');
$table->string('phone')->unique();
$table->date('dob')->format('d/m/Y');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
【问题讨论】:
-
格式化日期通常在应用层完成——绝对不在数据库中。我很确定至少没有办法在 MySQL 中指定日期 col 的格式。在the Laravel migration docs 中也没有提到
format()修饰符。我的猜测是您的format()只是被忽略了,并且正在创建一个正常的datecol。最好的解决方案是castyour date to the format you want。
标签: laravel-6 laravel-migrations