如何删除"updated_at"同时保持"created_at"在雄辩的ORM Laravel 5.8中 [英] How to remove "updated_at" while keeping the "created_at" in Eloquent ORM Laravel 5.8

查看:47
本文介绍了如何删除"updated_at"同时保持"created_at"在雄辩的ORM Laravel 5.8中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5.8中,我有一个仅创建但未更新的模型.因此,我只想删除 updated_at 字段.但是 created_at 字段是必填字段.

In Laravel 5.8 I have a model that is created only but not updated. So, I want to remove only the updated_at field. But the created_at field is required.

那么如何在保留 created_at 字段的同时仅删除 updated_at 字段?

So how can I remove only the updated_at field while keeping the created_at field?

推荐答案

在模型中添加以下两行:

In your model add these two lines:

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(); 
}

有关laravel时间戳的更多信息请参阅

for more info about laravel timestamp see

这篇关于如何删除"updated_at"同时保持"created_at"在雄辩的ORM Laravel 5.8中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆