S
S
Sergey2019-04-30 09:35:07
Laravel
Sergey, 2019-04-30 09:35:07

How to change date format in Laravel model?

The Laravel documentation says about the model property $dateFormat that this property determines how date attributes are stored in the database.
I use this property like this because I want only the date to be stored, not the time. But as a result, working with the date becomes impossible, i.e. even with a simple output it gives an errorprotected $dateFormat = 'Y-m-d'$model->created_at

Trailing data {"exception":"[object] (InvalidArgumentException(code: 0): Trailing data at ...\www\\vendor\
esbot\\carbon\\src\\Carbon\\Carbon.php:910)...

Those. the date of such a model is no longer perceived as a Carbon object, but, apparently, as a string, and any manipulations with the date as a date are no longer possible. Right?
Who can tell where to read about this, and how to change the date format in which the date is written to the database, while leaving it in the Carbon format?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Popov, 2019-04-30
@SVZhidkow

Question: why change the date entered into the database, if it is enough to change its presentation in appearance?
There is such an option - add to the model

protected $casts = [
    'created_at' => 'datetime:d/m/Y', // Свой формат
    'updated_at' => 'datetime:d/m/Y',
];

Well, read this chapter of the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question