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;
To disable only updated_at, you can override Model::setUpdatedAt() method, like following :
Of course, if you wanted to do this for the created_at column, it's just as easy. And that's work for Laravel 5.1
In Laravel 5.7, this was enough for me to have it work:
In migration:
instead of classic
$table->timestamp('created_at');
In model:
For Laravel 5.*
Model:
Migration:
In 5.4, the problem it gives is that it also doesn't populate updated_at field even after update (Eloquent update).
instead add this method
A solution that is simple, decoupled, and reusable is to use a Model Observer. The idea is to capture the
creating
event and fill thecreated_at
attribute. This method may be used by any number of models without having to repeat code or using unofficial tricks. Most importantly, it closely resembles the internal mechanics of the Model class, thus avoiding unexpected errors.1) Create
SetCreatedAt
observer inApp\Observers
:2) On
App\Providers\AppServiceProvider
inside theboot
method add the following line for each of the models that you want thecreated_at
to be generated for:3) On your models, the only thing that you have to do is disable the timestamps:
Tested with Laravel 5.3, but it should work with previous versions as well.
Good luck!