Is there a way to automatically cast a model's attribute whose type is time
in the same way we do with date
or datetime
?
I know about mutators, but I wonder if it can be done in a cleaner way, like using $casts
property.
Is there a way to automatically cast a model's attribute whose type is time
in the same way we do with date
or datetime
?
I know about mutators, but I wonder if it can be done in a cleaner way, like using $casts
property.
As of Laravel 5.6, it is possible to use $casts
to specify custom formats for dates & times. (relevant docs)
You'd want something like this:
protected $casts = [
'created_at' => 'datetime:H:i',
];
© 2022 - 2024 — McMap. All rights reserved.
protected $dates = ['nameofdatefield', 'nameofyourdatetimefield', 'nameoftimefield];
in your model. – CryptH:i:s
and I want it asH:i
. – Cady