Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roman_paprotsky's avatar

Cast a date for Laravel Nova, but return my own format

I have a Model that I want to use in Nova that has some data and datetime fields. Nova keeps complaining that I need to cast it to a Date type in my model, but I want to always return the format of dates as "Y-m-d" and datetimes as "Y-m-d H:i:s". I tried to cast the fields, and then edit the get attribute methods as follows, but the getAttribute methods seem to overwrite the casts:

protected $casts = [
    'date' => 'date',
    'start_time' => 'datetime',
    'end_time' => 'datetime',
    'actual_start_time' => 'datetime',
    'actual_end_time' => 'datetime'
];

...

public function getDateAttribute($value)
{
    return Carbon::parse($value)->format('Y-m-d');
}

public function getStartTimeAttribute($value) {
    return Carbon::parse($value)->format('Y-m-d H:i:s');
}

public function getEndTimeAttribute($value)
{
    return Carbon::parse($value)->format('Y-m-d H:i:s');
}

...

How can I cast to a date so Nova will be happy, but also always return in my desired format when returning the model instance to endpoints?

0 likes
2 replies
neilstee's avatar

@roman_paprotsky this looks good tho?

date is actually Y-m-d format, and datetime is Y-m-d H:i:s

So you don't need to edit the getAttribute, casting should be enough.

roman_paprotsky's avatar

It displays as an ISO timestamp. I've worked around this by casting at return in my controllers.

Please or to participate in this conversation.