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

sinisa's avatar

Nova: datetime field must cast to 'datetime' in eloquent model

So, I want to use a DateTime field in nova, and I have complaint that "datetime field must cast to 'datetime' in eloquent model". The problem is that field is a datetime field in database, and it is casted as well within the model.

If I change the cast to "date" instead, it works, but of time part gets nulled on update and I have no use. Anyone noticed this behaviour?

0 likes
6 replies
wingly's avatar

Can you show us the relevant code in the model (the cast part) and the Nova resource field?

sinisa's avatar

@wingly

From model:

protected $casts = [
        'start_date' => 'datetime',
        'end_date' => 'datetime',
        'venue_id' => 'int',
        'messageboard_id' => 'int',
        'is_permanent' => 'int',
    ];

Resource:

            DateTime::make(__('Start Date'),'start_date')->sortable(),
            DateTime::make(__('End Date'),'end_date')->sortable(),
jlrdw's avatar

Nova has both, double check that you have DateTime in nova, just suggestion.

sinisa's avatar

@jlrdw field is there, it works ok, so Nova has a proper field classes in. In case I change the model and place

protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'venue_id' => 'int',
        'messageboard_id' => 'int',
        'is_permanent' => 'int',
    ];

then Nova has no issues, just that when you save changes only date is being saved because of casting.

Or were you talking about something else?

sinisa's avatar

@jlrdw so I tried to cast to immutable_datatime and that one works, datetime casting fails. but for now this fixes my issue.

Please or to participate in this conversation.