laravel-6 - 在 Laravel 迁移中更改日期格式
问题描述
我想将日期格式从迁移更改1990-01-30
为30/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();
});
}
解决方案
在模型中声明:
class ModelName extends Model
{
protected $casts = [
'created_at' => 'datetime:d/m/Y', // Change your format
'updated_at' => 'datetime:d/m/Y',
];
}
推荐阅读
- react-native - 带有本机反应的过滤器按钮
- php - 无法在 php 中连接到 mysql 8
- cypress - 赛普拉斯如何选择包含文本的选项
- php - 使用 codeigniter 的简单示例 php 无法访问错误消息
- sql - 在 SQL Server 中的组内传输特定信息
- node.js - 无法在 Ubuntu 上启动 Angular 6 应用程序 - 详细堆栈退出状态 137
- owl - 如何在 Protégé 中输入电子邮件地址作为对象?
- mongodb - 如何使用 Talend OS for Big Data 将参数从 tMongoDBInput 传递到 tRestClient
- android - Android App Bundle 会自动从下载的 APK 加载类吗?
- arrays - numpy astype 对象到 int 的转换而不引发错误 python 2