diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 335bda3d..c2994da6 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -141,9 +141,11 @@ protected function buildProperties(Model $model) $properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns), $this->files->stub('model.casts.stub')); } - $columns = $this->dateColumns($model->columns()); - if (! empty($columns)) { - $properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model.dates.stub')); + if (! Blueprint::isLaravel8OrHigher()) { + $columns = $this->dateColumns($model->columns()); + if (! empty($columns)) { + $properties .= PHP_EOL.str_replace('[]', $this->pretty_print_array($columns, false), $this->files->stub('model.dates.stub')); + } } return trim($properties); @@ -285,6 +287,20 @@ function (Column $column) { private function castForColumn(Column $column) { + if (Blueprint::isLaravel8OrHigher()) { + if ($column->dataType() === 'date') { + return 'date'; + } + + if (stripos($column->dataType(), 'datetime') !== false) { + return 'datetime'; + } + + if (stripos($column->dataType(), 'timestamp') !== false) { + return 'timestamp'; + } + } + if (stripos($column->dataType(), 'integer') || $column->dataType() === 'id') { return 'integer'; } diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index fae6ed35..2d51ff8e 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -69,12 +69,6 @@ public function output_generates_models($definition, $path, $model) ->with('model.casts.stub') ->andReturn($this->stub('model.casts.stub')); - if (in_array($definition, ['drafts/readme-example.yaml', 'drafts/all-column-types.yaml'])) { - $this->files->expects('stub') - ->with('model.dates.stub') - ->andReturn($this->stub('model.dates.stub')); - } - $this->files->shouldReceive('stub') ->with('model.method.stub') ->andReturn($this->stub('model.method.stub')); @@ -214,9 +208,6 @@ public function output_works_for_pascal_case_definition() ->with('model.casts.stub') ->andReturn($this->stub('model.casts.stub')) ->twice(); - $this->files->expects('stub') - ->with('model.dates.stub') - ->andReturn(file_get_contents('stubs/model.dates.stub')); $this->files->expects('stub') ->with('model.method.stub') ->andReturn($this->stub('model.method.stub')) @@ -421,12 +412,6 @@ public function output_generates_phpdoc_for_model($definition, $path, $model) ->with('model.casts.stub') ->andReturn($this->stub('model.casts.stub')); - if ($definition === 'drafts/readme-example.yaml') { - $this->files->expects('stub') - ->with('model.dates.stub') - ->andReturn($this->stub('model.dates.stub')); - } - $this->files->shouldReceive('stub') ->with('model.method.stub') ->andReturn($this->stub('model.method.stub')); diff --git a/tests/fixtures/models/all-column-types-laravel8.php b/tests/fixtures/models/all-column-types-laravel8.php index 7e935f53..38b2b12a 100644 --- a/tests/fixtures/models/all-column-types-laravel8.php +++ b/tests/fixtures/models/all-column-types-laravel8.php @@ -75,12 +75,18 @@ class AllType extends Model protected $casts = [ 'bigInteger' => 'integer', 'boolean' => 'boolean', + 'date' => 'date', + 'dateTime' => 'datetime', + 'dateTimeTz' => 'datetime', 'decimal' => 'decimal', 'double' => 'double', 'float' => 'float', 'json' => 'array', 'mediumInteger' => 'integer', + 'nullableTimestamps' => 'timestamp', 'smallInteger' => 'integer', + 'timestamp' => 'timestamp', + 'timestampTz' => 'timestamp', 'tinyInteger' => 'integer', 'unsignedBigInteger' => 'integer', 'unsignedDecimal' => 'decimal', @@ -90,20 +96,6 @@ class AllType extends Model 'unsignedTinyInteger' => 'integer', ]; - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = [ - 'date', - 'dateTime', - 'dateTimeTz', - 'nullableTimestamps', - 'timestamp', - 'timestampTz', - ]; - public function uuid() { diff --git a/tests/fixtures/models/certificate-pascal-case-example-laravel8.php b/tests/fixtures/models/certificate-pascal-case-example-laravel8.php index e9e4668a..2ff5c42f 100644 --- a/tests/fixtures/models/certificate-pascal-case-example-laravel8.php +++ b/tests/fixtures/models/certificate-pascal-case-example-laravel8.php @@ -31,15 +31,7 @@ class Certificate extends Model protected $casts = [ 'id' => 'integer', 'certificate_type_id' => 'integer', - ]; - - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = [ - 'expiry_date', + 'expiry_date' => 'date', ]; diff --git a/tests/fixtures/models/readme-example-laravel8.php b/tests/fixtures/models/readme-example-laravel8.php index 2bc4e291..dc07e010 100644 --- a/tests/fixtures/models/readme-example-laravel8.php +++ b/tests/fixtures/models/readme-example-laravel8.php @@ -28,18 +28,10 @@ class Post extends Model */ protected $casts = [ 'id' => 'integer', + 'published_at' => 'timestamp', 'author_id' => 'integer', ]; - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = [ - 'published_at', - ]; - public function author() { diff --git a/tests/fixtures/models/readme-example-phpdoc-laravel8.php b/tests/fixtures/models/readme-example-phpdoc-laravel8.php index 5dc2245f..5c524d66 100644 --- a/tests/fixtures/models/readme-example-phpdoc-laravel8.php +++ b/tests/fixtures/models/readme-example-phpdoc-laravel8.php @@ -37,18 +37,10 @@ class Post extends Model */ protected $casts = [ 'id' => 'integer', + 'published_at' => 'timestamp', 'author_id' => 'integer', ]; - /** - * The attributes that should be mutated to dates. - * - * @var array - */ - protected $dates = [ - 'published_at', - ]; - /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo