llanfair
llanfair

Reputation: 1885

Laravel Model - cast of time

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.

Upvotes: 6

Views: 6259

Answers (1)

Sara
Sara

Reputation: 8242

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',
];

Upvotes: 15

Related Questions