I want only use created_at , how to do it?
I know:
This can custom timestamps name
const CREATED_AT = 'created';
const UPDATED_AT = 'updated';
This can disable timestamps
public $timestamps = false;
I want only use created_at , how to do it?
I know:
This can custom timestamps name
const CREATED_AT = 'created';
const UPDATED_AT = 'updated';
This can disable timestamps
public $timestamps = false;
Approach with only
setUpdatedAt
did not work with Laravel 5.1.7 because it has one more place whereupdated_at
gets processed.Model class performUpdate method calls Builder class method:
which leads us to
I'm not sure, why Laravel is processing updated_at twice - in
performUpdate
with$this->updateTimestamps()
and then later in Builder with$this->addUpdatedAtColumn($values)
.With some debugging, I found that you have to also update your model with getUpdatedAtColumn override. At first I was afraid that it will try to update a non-existent field "null", but it turned out that
Arr::add
is smart enough to ignore null keys.So, in your model class add these:
This should be enough to disable both updates.
My solution for this is using a new
__construct
method.See:
I solved by adding default value
CURRENT_TIMESTAMP
in my database for the columncreated_at
. And In my model I use this below codeI hope this method works in all versions of laravel.
You can use CURRENT_TIMESTAMP default value for
created
field in your MySQL table and setin your model.
In version 5.3 I just did
public $timestamps = false;
and then added protected $fillable = ['created_at']. Note: You can add anything you want, just make sure it matches the one in your table.`Use on the top the class:
const UPDATED_AT = null;
or
const CREATED_AT = null;