Laravel Model - cast of time
Asked Answered
C

1

6

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.

Cady answered 6/7, 2018 at 23:36 Comment(2)
Just add those as protected $dates = ['nameofdatefield', 'nameofyourdatetimefield', 'nameoftimefield]; in your model.Crypt
Does not work, as mentioned. My field type if on the format H:i:s and I want it as H:i.Cady
D
13

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',
];
Durant answered 2/4, 2019 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.