Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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';
}
Expand Down
15 changes: 0 additions & 15 deletions tests/Feature/Generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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'));
Expand Down
20 changes: 6 additions & 14 deletions tests/fixtures/models/all-column-types-laravel8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];


Expand Down
10 changes: 1 addition & 9 deletions tests/fixtures/models/readme-example-laravel8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
10 changes: 1 addition & 9 deletions tests/fixtures/models/readme-example-phpdoc-laravel8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down