问题描述
我是Laravel的新手.
I am new to Laravel.
我想使用自定义字段名称(例如ct
,而不使用updated_at
)为Laravel中的所有插入操作添加时间戳.
I want to add timestamp for all insert actions in Laravel using custom field name such as ct
and not using updated_at
.
我想出了下面的方法,但是没有运气.我所有的表都具有带有not null
属性的ct
字段.
I came up with below, but without luck. All of my tables have ct
field with not null
property.
class ProductLog extends Model
{
const CREATED_AT = 'ct';
public $timestamps = false;
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = $model->freshTimestamp(); => 1)
$model->setCreatedAt($model->freshTimestamp()); => 2)
});
}
}
1)和2)都仍然抛出一个错误,说'ct'没有默认值.
Both of 1) and 2) still throws me an error saying 'ct' doesn't have a default value.
任何建议或建议都将不胜感激.
Any suggestion or advice would be appreciated.
谢谢.
推荐答案
我刚刚对此进行了测试,并且效果很好:
I've just tested this and it works perfectly:
const UPDATED_AT = null;
const CREATED_AT = 'ct';
无需执行任何其他操作.使用此解决方案,Laravel将ct
用作created_at
,并且不会将updated_at
添加到任何更新查询中.
There's no need to do anything else. With this solution, Laravel will use ct
as created_at
and will not add updated_at
to any update queries.
这篇关于自定义created_at,仅使用自定义created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!