'datetime' casted attribute returning current date from am empty string in the db #51507
-
Laravel Version11.5.0 PHP Version8.3.4 Database Driver & VersionSQLite 3.19.1 for Windows 11 DescriptionWhen an empty string is persisted on a datetime column in the database and that column is casted by 'datetime' cast, you get current datetime instead of null as the output. Steps To Reproduce
Expected result: null or '' |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 10 replies
-
Shouldn't it be |
Beta Was this translation helpful? Give feedback.
-
Using Livewire for example, if you set a date on the standard html datepicker and then clear it, then an empty string is passed instead of null. I don't know what happens with just regular blade code, didn't try it. |
Beta Was this translation helpful? Give feedback.
-
If you use the validator with nullable rule and retrieve the validated fields that should convert empty strings to null |
Beta Was this translation helpful? Give feedback.
-
Already did that, no difference. |
Beta Was this translation helpful? Give feedback.
-
This seems to be a Livewire issue instead of a bug for Laravel Framework. Feel free to continue the discussion for now. |
Beta Was this translation helpful? Give feedback.
-
The issue comes from Carbon. Carbon::parse('') returns now. This is why I never cast anything and use anything that comes from db as string, int or null |
Beta Was this translation helpful? Give feedback.
-
This is because you are using SQLite and you are not forcing strict types on the table. In MySQL and Postgres '' is not a valid date, but in SQLite as by default types are just a suggestion and they are not enforced it will be saved as empty string and parsed as such. If you plan to use SQLite in product enforce strict types in your table or validate the input better. |
Beta Was this translation helpful? Give feedback.
The issue comes from Carbon. Carbon::parse('') returns now.
https://play.phpsandbox.io/nesbot/carbon
This is why I never cast anything and use anything that comes from db as string, int or null