in some tests running cast with date type in the model I noticed that no matter how long the table field is date the value returned is in datetime format. I believe something is wrong because if it was defined in the cast datetime (according to the documentation) then yes it would return in this format, correct?
set in variable $cast any value as date and run your code getting the table value through eloquent
What data type do you expect to get from a date cast? Is there any date type in php or Carbon?
I expected this interpretation:
date = Y-m-d
datetime = Y-m-d H:i:s
The date type cast simply cast the value to datetime and then calls ->startOfDay() on it to zero the time component. This is the closest you can get to Date type in php.
I just noticed this behavior as well. It feels a bit weird to me since I'm storing the timestamp in a date type column.
Since we're using Carbon here, I think it would be logical for the date type cast to use ->toDateString().
There's a discussion on that topic already https://github.com/laravel/internals/issues/669
I don't think this is the same thing as laravel/ideas#669. That idea and the implemented solution work well for storing and serializing, but it does not cover use of the field as a string (echo $model->date;)
The root issue is that the $dateFormat attribute takes effect on both date and datetime casts. Assigning a value of '2019-06-10' to an attribute with a date cast shouldn't result in '2019-06-10 00:00:00' output, but here we are.
This is still not working but Nova wants you to cast to date, but then in getter, it outputs timestamp. (not working in input type=date)
For anyone else who came across this old issue from a Google search, as of Laravel 5.6 (I think), you can now specify the date/datetime formats like this:
protected $casts = [
'birthday' => 'date:Y-m-d',
'joined_at' => 'datetime:Y-m-d H:00',
];
https://laravel.com/docs/5.8/eloquent-serialization#date-serialization
thanks @orrd .
Its work for me.
@orrd Do you have any ideas to convert to this format:
"2020-10-29T00:00"
Thanks.
Most helpful comment
For anyone else who came across this old issue from a Google search, as of Laravel 5.6 (I think), you can now specify the date/datetime formats like this:
https://laravel.com/docs/5.8/eloquent-serialization#date-serialization