如何在雄辩的ORM Laravel 5.8中保留“ created_at”的同时删除“ updated_at”

问题描述 投票:2回答:2

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

那么如何在保留updated_at字段的情况下仅删除created_at字段?

php laravel eloquent timestamp laravel-5.8
2个回答
2
投票

在模型中添加以下两行:

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
投票

将下面的行添加到您的模型中。这适用于插入和选择查询。

public $timestamps = false;

如果也不需要创建列,请在数据库/迁移文件中删除以下行。

$table->timestamps();
© www.soinside.com 2019 - 2024. All rights reserved.