Skip to content

Commit 1a3e042

Browse files
author
Brian Faust
authored
Use morphs instead of manually building columns (#627)
1 parent d427d17 commit 1a3e042

File tree

4 files changed

+6
-12
lines changed

4 files changed

+6
-12
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ protected function buildDefinition(Model $model)
270270

271271
if (array_key_exists('morphTo', $relationships)) {
272272
foreach ($relationships['morphTo'] as $morphTo) {
273-
$definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($morphTo . '_id')) . PHP_EOL;
274-
$definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($morphTo . '_type')) . PHP_EOL;
273+
$definition .= self::INDENT . sprintf('$table->morphs(\'%s\');', Str::lower($morphTo)) . PHP_EOL;
275274
}
276275
}
277276

@@ -332,8 +331,7 @@ protected function buildPolyTableDefinition(string $parentTable)
332331
$definition .= self::INDENT . '$table->foreignId(\'' . $foreign . '\');' . PHP_EOL;
333332
}
334333

335-
$definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower(Str::singular($parentTable) . 'able' . '_id')) . PHP_EOL;
336-
$definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower(Str::singular($parentTable) . 'able' . '_type')) . PHP_EOL;
334+
$definition .= self::INDENT . sprintf('$table->morphs(\'%s\');', Str::lower(Str::singular($parentTable) . 'able')) . PHP_EOL;
337335

338336
return trim($definition);
339337
}

tests/fixtures/migrations/morphed-by-many-intermediate.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public function up(): void
1313
{
1414
Schema::create('tagables', function (Blueprint $table) {
1515
$table->foreignId('tag_id');
16-
$table->unsignedBigInteger('tagable_id');
17-
$table->string('tagable_type');
16+
$table->morphs('tagable');
1817
});
1918
}
2019

tests/fixtures/migrations/polymorphic_relationships_images_table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public function up(): void
1414
Schema::create('images', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('url', 400);
17-
$table->unsignedBigInteger('imageable_id');
18-
$table->string('imageable_type');
17+
$table->morphs('imageable');
1918
$table->timestamps();
2019
});
2120
}

tests/fixtures/migrations/polymorphic_relationships_images_table_multiple_morphto.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public function up(): void
1414
Schema::create('images', function (Blueprint $table) {
1515
$table->id();
1616
$table->string('url', 400);
17-
$table->unsignedBigInteger('imageable_id');
18-
$table->string('imageable_type');
19-
$table->unsignedBigInteger('tagable_id');
20-
$table->string('tagable_type');
17+
$table->morphs('imageable');
18+
$table->morphs('tagable');
2119
$table->timestamps();
2220
});
2321
}

0 commit comments

Comments
 (0)