Ok, found the problem 3 seconds after posting!
I was using:
protected $casts = [
'datetime' => 'date'
];
This should be:
protected $casts = [
'datetime' => 'datetime'
];
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm stuck with this one:
I have a database that contains a DATETIME field, also called 'datetime'. I have the filed cast to a date via the model.
If I do a $foobar->getAttributes() I get:
Array
(
[id] => 123
[datetime] => 2018-02-10 12:34:56
[created_at] => 2018-02-10 14:26:50
[updated_at] => 2018-02-10 14:26:50
)
That seems great, as that matches what I see in the database.
However, when I output the Carbon date object $foobar->datetime, I get:
Illuminate\Support\Carbon Object
(
[date] => 2018-02-10 00:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
And subsequently $foobar->datetime->toDateTimeString() outputs:
2018-02-10 00:00:00
So as you can see, the 'time' portion of the datetime value (12:34:56) is somehow lost when casting it to the Carbon instance.
Anybody got a clue why this is?
Please or to participate in this conversation.