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;
Eloquent does not provide such functionality out of the box, but you can create it on your own using the
creating
event callback:I used very simple hack ;)
class MyClass extends Model {
}
I just pointed both to the same column :)
Use on the top:
And for 'created_at' field, you can use:
UPDATE FOR LARAVEL 5.5.0 - 5.5.5
This was broken in Laravel 5.5.0 (and fixed again in 5.5.5).
If you are using 5.5.x, make sure you are on the newest version.
If for some reason you cannot be on the newest version, here is a workaround.
Set the public $timestamps to false:
And when necessary:
OR
When the two fields "created_at" and "updated_at" are required, you do not have to do anything, of course ;)
I have a better answer from here for Laravel 5.2 or above.
U can use this in model-
So, for your question, the answer is -
My solution:
This works for me
On your
model
setconst UPDATED_AT = null;
or
const CREATED_AT = null;
it stops Laravel trying to update the updated_at/created_at field
It works for Laravel 5.8
And it is better than overwrite:
setUpdatedAt
in yourmodel
because it is shorter and because the check for
if (! is_null(static::UPDATED_AT)
is happening in source code earlier than triggeringsetUpdatedAt