diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index 8701f734..450e6ac3 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -184,10 +184,10 @@ private function buildModel(string $name, array $columns) $model->addColumn($column); $foreign = collect($column->modifiers())->filter(function ($modifier) { - return (is_array($modifier) && key($modifier) === 'foreign') || $modifier === 'foreign'; + return collect($modifier)->containsStrict('foreign') || collect($modifier)->has('foreign'); })->flatten()->first(); - if ($column->name() !== 'id' && (in_array($column->dataType(), ['id', 'uuid']) || $foreign)) { + if (($column->name() !== 'id') && ($column->dataType() === 'id') || $foreign) { $reference = $column->name(); if ($foreign && $foreign !== 'foreign') { diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index fb7a0645..5381e384 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -795,6 +795,7 @@ public function modelTreeDataProvider() ['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'], ['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'], ['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'], + ['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'], ['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'], ['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'], ['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'], diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index 2d51ff8e..f1ddd3e9 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -661,6 +661,7 @@ public function modelTreeDataProvider() ['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements.php'], ['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types.php'], ['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships.php'], + ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'], ]; } @@ -687,6 +688,7 @@ public function laravel8ModelTreeDataProvider() ['drafts/resource-statements.yaml', 'app/User.php', 'models/resource-statements-laravel8.php'], ['drafts/all-column-types.yaml', 'app/AllType.php', 'models/all-column-types-laravel8.php'], ['drafts/alias-relationships.yaml', 'app/Salesman.php', 'models/alias-relationships-laravel8.php'], + ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship-laravel8.php'], ]; } diff --git a/tests/fixtures/drafts/uuid-shorthand-invalid-relationship.yaml b/tests/fixtures/drafts/uuid-shorthand-invalid-relationship.yaml new file mode 100644 index 00000000..5f28ac45 --- /dev/null +++ b/tests/fixtures/drafts/uuid-shorthand-invalid-relationship.yaml @@ -0,0 +1,9 @@ +models: + AgeCohort: + id + timestamps + name: string:100 + description: string:500 nullable + min_age: integer nullable + max_age: integer nullable + uuid: uuid unique \ No newline at end of file diff --git a/tests/fixtures/migrations/uuid-shorthand-invalid-relationship.php b/tests/fixtures/migrations/uuid-shorthand-invalid-relationship.php new file mode 100644 index 00000000..a30faf02 --- /dev/null +++ b/tests/fixtures/migrations/uuid-shorthand-invalid-relationship.php @@ -0,0 +1,36 @@ +id(); + $table->string('name', 100); + $table->string('description', 500)->nullable(); + $table->integer('min_age')->nullable(); + $table->integer('max_age')->nullable(); + $table->uuid('uuid')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('age_cohorts'); + } +} diff --git a/tests/fixtures/models/all-column-types-laravel8.php b/tests/fixtures/models/all-column-types-laravel8.php index 38b2b12a..ae49b553 100644 --- a/tests/fixtures/models/all-column-types-laravel8.php +++ b/tests/fixtures/models/all-column-types-laravel8.php @@ -95,10 +95,4 @@ class AllType extends Model 'unsignedSmallInteger' => 'integer', 'unsignedTinyInteger' => 'integer', ]; - - - public function uuid() - { - return $this->belongsTo(\App\Uuid::class); - } } diff --git a/tests/fixtures/models/all-column-types.php b/tests/fixtures/models/all-column-types.php index bcefe7c7..ce748208 100644 --- a/tests/fixtures/models/all-column-types.php +++ b/tests/fixtures/models/all-column-types.php @@ -100,10 +100,4 @@ class AllType extends Model 'timestamp', 'timestampTz', ]; - - - public function uuid() - { - return $this->belongsTo(\App\Uuid::class); - } } diff --git a/tests/fixtures/models/uuid-shorthand-invalid-relationship-laravel8.php b/tests/fixtures/models/uuid-shorthand-invalid-relationship-laravel8.php new file mode 100644 index 00000000..ef5639a3 --- /dev/null +++ b/tests/fixtures/models/uuid-shorthand-invalid-relationship-laravel8.php @@ -0,0 +1,33 @@ + 'integer', + ]; +} diff --git a/tests/fixtures/models/uuid-shorthand-invalid-relationship.php b/tests/fixtures/models/uuid-shorthand-invalid-relationship.php new file mode 100644 index 00000000..998f389d --- /dev/null +++ b/tests/fixtures/models/uuid-shorthand-invalid-relationship.php @@ -0,0 +1,30 @@ + 'integer', + ]; +}