diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 09a3fb1f..c281e2eb 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -294,6 +294,9 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ } elseif (Str::contains($on, '.')) { [$table, $column] = explode('.', $on); $table = Str::snake($table); + } elseif (Str::contains($on, '\\')) { + $table = Str::lower(Str::plural(Str::afterLast($on, '\\'))); + $column = Str::afterLast($column_name, '_'); } else { $table = Str::plural($on); $column = Str::afterLast($column_name, '_'); diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index aeb66e48..b1bf9d56 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -604,6 +604,7 @@ public function modelTreeDataProvider() ['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'], ['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'], ['drafts/boolean-column-default.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/boolean-column-default.php'], + ['drafts/foreign-with-class.yaml', 'database/migrations/timestamp_create_events_table.php', 'migrations/foreign-with-class.php'], ]; } } diff --git a/tests/fixtures/drafts/foreign-with-class.yaml b/tests/fixtures/drafts/foreign-with-class.yaml new file mode 100644 index 00000000..f294edaa --- /dev/null +++ b/tests/fixtures/drafts/foreign-with-class.yaml @@ -0,0 +1,4 @@ +models: + Event: + working_title: string + image_id: uuid foreign:\Tests\Models\Attachment nullable diff --git a/tests/fixtures/migrations/foreign-with-class.php b/tests/fixtures/migrations/foreign-with-class.php new file mode 100644 index 00000000..039dbd68 --- /dev/null +++ b/tests/fixtures/migrations/foreign-with-class.php @@ -0,0 +1,37 @@ +id(); + $table->string('working_title'); + $table->foreignUuid('image_id')->nullable()->constrained('attachments'); + $table->timestamps(); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('events'); + } +}