在Laraval 5.8中,我有一个仅创建但未更新的模型。因此,我只想删除updated_at
字段。但是created_at
字段为必填。
那么如何在保留updated_at
字段的情况下仅删除created_at
字段?
在模型中添加以下两行:
public $timestamps = ["created_at"]; //only want to used created_at column
const UPDATED_AT = null; //and updated by default null set
第二种方式:
public $timestamps = false; //by default timestamp false
添加这样的功能:
public function setCreatedAtAttribute($value) {
$this->attributes['created_at'] = \Carbon\Carbon::now();
}
将下面的行添加到您的模型中。这适用于插入和选择查询。
public $timestamps = false;
如果也不需要创建列,请在数据库/迁移文件中删除以下行。
$table->timestamps();