Skip to content

Commit ae0b14a

Browse files
Align code and docs for unconventional foreign keys (#737)
1 parent d0a7256 commit ae0b14a

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,11 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
275275
$column = Str::afterLast($column_name, '_');
276276
} elseif (Str::contains($on, '.')) {
277277
[$table, $column] = explode('.', $on);
278-
$table = Str::snake($table);
279278
} elseif (Str::contains($on, '\\')) {
280-
$table = Str::lower(Str::plural(Str::afterLast($on, '\\')));
279+
$table = Str::snake(Str::plural(Str::afterLast($on, '\\')));
281280
$column = Str::afterLast($column_name, '_');
282281
} else {
283-
$table = Str::plural($on);
282+
$table = Str::snake(Str::plural($on));
284283
$column = Str::afterLast($column_name, '_');
285284
}
286285

@@ -315,12 +314,15 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
315314
if ($on_delete_clause === 'cascade') {
316315
$on_delete_suffix = '->cascadeOnDelete()';
317316
}
317+
318318
if ($on_update_clause === 'cascade') {
319319
$on_update_suffix = '->cascadeOnUpdate()';
320320
}
321+
321322
if ($column_name === Str::singular($table) . '_' . $column) {
322323
return self::INDENT . "{$prefix}->constrained(){$on_delete_suffix}{$on_update_suffix}";
323324
}
325+
324326
if ($column === 'id') {
325327
return self::INDENT . "{$prefix}->constrained('{$table}'){$on_delete_suffix}{$on_update_suffix}";
326328
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
models:
22
State:
33
name: string
4-
countries_id: id foreign:countries
5-
country_code: string foreign:countries
6-
ccid: string foreign:countries
4+
countries_id: id foreign:country
5+
country_code: string foreign:Country
6+
ccid: string foreign:country.id
77
c_code: string foreign:countries.code

tests/fixtures/factories/unconventional-foreign-key.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function definition(): array
2525
'name' => fake()->name(),
2626
'countries_id' => Country::factory(),
2727
'country_code' => Country::factory()->create()->code,
28-
'ccid' => Country::factory()->create()->ccid,
28+
'ccid' => Country::factory(),
2929
'c_code' => Country::factory()->create()->code,
3030
];
3131
}

tests/fixtures/migrations/unconventional-foreign-key.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function up(): void
2020
$table->string('country_code');
2121
$table->foreign('country_code')->references('code')->on('countries');
2222
$table->string('ccid');
23-
$table->foreign('ccid')->references('ccid')->on('countries');
23+
$table->foreign('ccid')->references('id')->on('country');
2424
$table->string('c_code');
2525
$table->foreign('c_code')->references('code')->on('countries');
2626
$table->timestamps();

0 commit comments

Comments
 (0)