diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 2bfed6d94f5c..22e02e2a7c54 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -2920,6 +2920,7 @@ protected function castAttribute($key, $value) case 'collection': return new BaseCollection($this->fromJson($value)); case 'date': + return $this->asDate($value); case 'datetime': return $this->asDateTime($value); case 'timestamp': @@ -3028,6 +3029,17 @@ public function fromDateTime($value) return $value->format($format); } + /** + * Return a timestamp as DateTime object with time set to 00:00:00. + * + * @param mixed $value + * @return \Carbon\Carbon + */ + protected function asDate($value) + { + return $this->asDateTime($value)->startOfDay(); + } + /** * Return a timestamp as DateTime object. * diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 97afc3597c23..542048336120 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1409,6 +1409,18 @@ public function testModelAttributesAreCastedWhenPresentInCastsArray() $this->assertEquals(-14173440, $arr['timestampAttribute']); } + public function testModelDateAttributeCastingResetsTime() + { + $model = new EloquentModelCastingStub; + $model->setDateFormat('Y-m-d H:i:s'); + $model->dateAttribute = '1969-07-20 22:56:00'; + + $this->assertEquals('1969-07-20 00:00:00', $model->dateAttribute->toDateTimeString()); + + $arr = $model->toArray(); + $this->assertEquals('1969-07-20 00:00:00', $arr['dateAttribute']); + } + public function testModelAttributeCastingPreservesNull() { $model = new EloquentModelCastingStub;