diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 26ae818f..5d75ecc8 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -271,14 +271,21 @@ protected function buildRelationships(Model $model): string protected function addTraits(Model $model, $stub): string { - if (!$model->usesSoftDeletes()) { - return $stub; + $traits = ['HasFactory']; + + if ($model->usesSoftDeletes()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); + $traits[] = 'SoftDeletes'; + } + + if ($model->usesUuids()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); + $traits[] = 'HasUuids'; } - $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); - $stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub); + sort($traits); - return $stub; + return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub); } private function fillableColumns(array $columns): array diff --git a/src/Models/Model.php b/src/Models/Model.php index 0ec12e81..cee66fd7 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -96,6 +96,11 @@ public function usesPrimaryKey(): bool return $this->primaryKey !== false; } + public function usesUuids(): bool + { + return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; + } + public function disablePrimaryKey(): void { $this->primaryKey = false; diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index fb6cfa1f..eb8b2b6f 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -595,6 +595,38 @@ public function output_generates_models_with_custom_pivot(): void $this->assertEquals(['created' => ['app/Models/User.php', 'app/Models/Team.php', 'app/Models/Membership.php']], $this->subject->output($tree)); } + #[Test] + public function output_generates_models_with_hasuuids_trait_if_uuid_id_is_type_uuid(): void + { + $this->filesystem->expects('stub') + ->with('model.class.stub') + ->andReturn($this->stub('model.class.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.casts.stub') + ->andReturn($this->stub('model.casts.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.fillable.stub') + ->andReturn($this->stub('model.fillable.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.method.stub') + ->andReturn($this->stub('model.method.stub')); + + $this->filesystem->expects('exists') + ->times(1) + ->with('app/Models') + ->andReturnTrue(); + + $this->filesystem->expects('put') + ->with('app/Models/User.php', $this->fixture('models/model-with-uuid-trait.php')); + $tokens = $this->blueprint->parse($this->fixture('drafts/model-with-uuid-id.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['app/Models/User.php']], $this->subject->output($tree)); + } + public static function modelTreeDataProvider(): array { return [ @@ -610,6 +642,7 @@ public static function modelTreeDataProvider(): array ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'], ['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'], ['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'], + ['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'], ]; } diff --git a/tests/fixtures/drafts/model-with-uuid-id.yaml b/tests/fixtures/drafts/model-with-uuid-id.yaml new file mode 100644 index 00000000..0f4e491d --- /dev/null +++ b/tests/fixtures/drafts/model-with-uuid-id.yaml @@ -0,0 +1,5 @@ +models: + User: + id: uuid primary + name: string + base_pay: decimal:10,2 diff --git a/tests/fixtures/models/model-with-uuid-trait.php b/tests/fixtures/models/model-with-uuid-trait.php new file mode 100644 index 00000000..c6ba2147 --- /dev/null +++ b/tests/fixtures/models/model-with-uuid-trait.php @@ -0,0 +1,31 @@ + 'decimal:2', + ]; +}