首页 > 解决方案 > 在 Laravel 迁移中更改日期格式

问题描述

我想将日期格式从迁移更改1990-01-3030/01/1990直接。当我尝试从工厂播种迁移时出现以下错误。

无效的日期时间格式:1292 不正确的日期值:第 1 行的列“dob”的“30/01/1990”

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();
    });
}

标签: laravel-6laravel-migrations

解决方案


在模型中声明:

class ModelName extends Model
{      

 protected $casts = [
    'created_at' => 'datetime:d/m/Y', // Change your format
    'updated_at' => 'datetime:d/m/Y',
];
}

推荐阅读