'datetime' casted attribute returning current date from am empty string in the db #51507
Replies: 5 comments 6 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.
-
Laravel Version
11.5.0
PHP Version
8.3.4
Database Driver & Version
SQLite 3.19.1 for Windows 11
Description
When 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
$table->dateTime('approved_at')->nullable();
protected function casts(): array { return [ 'approved_at' => 'datetime', ]; }
Post::create(['title' => 'Laravel', 'approved_at' => '']);
$post->approved_at;
Expected result: null or ''
Outcome: current datetime (i.e. same as now() method)
Beta Was this translation helpful? Give feedback.
All reactions