在 Laravel 迁移中更改日期格式
Change date format in Laravel migration
我想在迁移后直接将日期格式从 1990-01-30
更改为 30/01/1990
。当我尝试使用工厂播种进行迁移时出现以下错误。
Invalid datetime format: 1292 Incorrect date value: '30/01/1990' for
column 'dob' at row 1
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();
});
}
您不能在迁移中执行此操作。您将需要使用 Carbon 并在您的模型中格式化日期。
在模型中声明:
class ModelName extends Model
{
protected $casts = [
'created_at' => 'datetime:d/m/Y', // Change your format
'updated_at' => 'datetime:d/m/Y',
];
}
我想在迁移后直接将日期格式从 1990-01-30
更改为 30/01/1990
。当我尝试使用工厂播种进行迁移时出现以下错误。
Invalid datetime format: 1292 Incorrect date value: '30/01/1990' for column 'dob' at row 1
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();
});
}
您不能在迁移中执行此操作。您将需要使用 Carbon 并在您的模型中格式化日期。
在模型中声明:
class ModelName extends Model
{
protected $casts = [
'created_at' => 'datetime:d/m/Y', // Change your format
'updated_at' => 'datetime:d/m/Y',
];
}