From 737bd83851427092145c8c39de1bee94d5d2c4e0 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 19 Jun 2017 12:42:04 +0200 Subject: [PATCH] fix updating nullable dates --- .../Eloquent/Concerns/HasAttributes.php | 7 ++- .../Database/EloquentModelTest.php | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/Integration/Database/EloquentModelTest.php diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 9e262c975617..7a6200aaaa58 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -728,9 +728,10 @@ protected function isStandardDateFormat($value) */ public function fromDateTime($value) { - return $this->asDateTime($value)->format( - $this->getDateFormat() - ); + return $value == null + ? $value : $this->asDateTime($value)->format( + $this->getDateFormat() + ); } /** diff --git a/tests/Integration/Database/EloquentModelTest.php b/tests/Integration/Database/EloquentModelTest.php new file mode 100644 index 000000000000..09645147085f --- /dev/null +++ b/tests/Integration/Database/EloquentModelTest.php @@ -0,0 +1,57 @@ +set('app.debug', 'true'); + + $app['config']->set('database.default', 'testbench'); + + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + public function setUp() + { + parent::setUp(); + + Schema::create('users', function ($table) { + $table->increments('id'); + $table->timestamp('nullable_date')->nullable(); + }); + } + + public function test_user_can_update_nullable_date() + { + $user = EloquentModelTestModel::create([ + 'nullable_date' => null, + ]); + + $user->fill([ + 'nullable_date' => $now = \Carbon\Carbon::now(), + ]); + $this->assertTrue($user->isDirty('nullable_date')); + + $user->save(); + $this->assertEquals($now->toDateString(), $user->nullable_date->toDateString()); + } +} + +class EloquentModelTestModel extends Model +{ + public $table = 'users'; + public $timestamps = false; + protected $guarded = ['id']; + protected $dates = ['nullable_date']; +}