From a4b23b8467b21b65a61361e564fba2381a60f454 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 1 Feb 2018 19:32:07 +0200 Subject: [PATCH 1/2] cast to date format --- .../Eloquent/Concerns/HasAttributes.php | 9 ++++ .../Database/EloquentModelDateCastingTest.php | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/Integration/Database/EloquentModelDateCastingTest.php diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 8edf5b29382b..cb0faf6ee743 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -189,6 +189,10 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt ($value === 'date' || $value === 'datetime')) { $attributes[$key] = $this->serializeDate($attributes[$key]); } + + if ($attributes[$key] && Str::startsWith($value, 'date:')) { + $attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]); + } } return $attributes; @@ -488,6 +492,7 @@ protected function castAttribute($key, $value) case 'date': return $this->asDate($value); case 'datetime': + case 'custom_datetime': return $this->asDateTime($value); case 'timestamp': return $this->asTimestamp($value); @@ -504,6 +509,10 @@ protected function castAttribute($key, $value) */ protected function getCastType($key) { + if (Str::startsWith($this->getCasts()[$key], 'date:')) { + return 'custom_datetime'; + } + return trim(strtolower($this->getCasts()[$key])); } diff --git a/tests/Integration/Database/EloquentModelDateCastingTest.php b/tests/Integration/Database/EloquentModelDateCastingTest.php new file mode 100644 index 000000000000..09991e63545b --- /dev/null +++ b/tests/Integration/Database/EloquentModelDateCastingTest.php @@ -0,0 +1,51 @@ +increments('id'); + $table->date('date_field')->nullable(); + $table->datetime('datetime_field')->nullable(); + }); + } + + public function test_user_can_update_nullable_date() + { + $user = TestModel1::create([ + 'date_field' => '2019-10-01', + 'datetime_field' => '2019-10-01 10:15:20', + ]); + + $this->assertEquals('2019-10', $user->toArray()['date_field']); + $this->assertEquals('2019-10 10:15', $user->toArray()['datetime_field']); + $this->assertInstanceOf(Carbon::class, $user->date_field); + $this->assertInstanceOf(Carbon::class, $user->datetime_field); + } +} + +class TestModel1 extends Model +{ + public $table = 'test_model1'; + public $timestamps = false; + protected $guarded = ['id']; + protected $dates = ['date_field', 'datetime_field']; + + public $casts = [ + 'date_field' => 'date:Y-m', + 'datetime_field' => 'date:Y-m H:i', + ]; +} From f8abc3f395458bdcd69e19a0b03f00eec828b290 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 1 Feb 2018 23:19:44 +0200 Subject: [PATCH 2/2] add datetime format as well --- src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php | 4 ++-- tests/Integration/Database/EloquentModelDateCastingTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index cb0faf6ee743..227f6df328e0 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -190,7 +190,7 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt $attributes[$key] = $this->serializeDate($attributes[$key]); } - if ($attributes[$key] && Str::startsWith($value, 'date:')) { + if ($attributes[$key] && Str::startsWith($value, ['date:', 'datetime:'])) { $attributes[$key] = $attributes[$key]->format(explode(':', $value, 2)[1]); } } @@ -509,7 +509,7 @@ protected function castAttribute($key, $value) */ protected function getCastType($key) { - if (Str::startsWith($this->getCasts()[$key], 'date:')) { + if (Str::startsWith($this->getCasts()[$key], ['date:', 'datetime:'])) { return 'custom_datetime'; } diff --git a/tests/Integration/Database/EloquentModelDateCastingTest.php b/tests/Integration/Database/EloquentModelDateCastingTest.php index 09991e63545b..ec7a41241245 100644 --- a/tests/Integration/Database/EloquentModelDateCastingTest.php +++ b/tests/Integration/Database/EloquentModelDateCastingTest.php @@ -46,6 +46,6 @@ class TestModel1 extends Model public $casts = [ 'date_field' => 'date:Y-m', - 'datetime_field' => 'date:Y-m H:i', + 'datetime_field' => 'datetime:Y-m H:i', ]; }