在Laravel 5.8中,我有一个仅创建但未更新的模型。因此,我只想删除updated_at
字段。但是created_at
字段是必填字段。
那么如何在保留updated_at
字段的同时仅删除created_at
字段?
答案 0 :(得分:3)
在模型中添加以下两行:
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();
}
答案 1 :(得分:1)
将以下行添加到模型中。这适用于插入和选择查询。
public $timestamps = false;
如果您也不需要创建列,请删除数据库/迁移文件中该行下方的内容。
$table->timestamps();
答案 2 :(得分:0)
你检查的答案不是问题所要求的。
您可以仅在表创建迁移中删除 updated_at
列:
Schema::create('sample', function (Blueprint $table)
$table->id();
$table->timestamps();
$table->dropColumn('updated_at');
});