Skip to content

Commit 2908957

Browse files
committed
fix(database): use dynamic table names for taxonomy foreign keys #10
1 parent 6740cb7 commit 2908957

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

database/migrations/2025_05_30_000000_create_taxonomies_tables.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99
public function up(): void
1010
{
1111
$tableNames = config('taxonomy.table_names');
12+
$tableNames = array_merge([
13+
'taxonomies' => 'taxonomies',
14+
'taxonomables' => 'taxonomables',
15+
], (array) config('taxonomy.table_names', []));
16+
1217
$morphType = config('taxonomy.morph_type', 'uuid');
1318

14-
Schema::create($tableNames['taxonomies'], function (Blueprint $table) {
19+
Schema::create($tableNames['taxonomies'], function (Blueprint $table) use ($tableNames) {
1520
$table->id();
1621
$table->string('name');
1722
$table->string('slug');
1823
$table->string('type')->index();
1924
$table->text('description')->nullable();
20-
$table->foreignId('parent_id')->nullable()->constrained('taxonomies');
25+
$table->foreignId('parent_id')->nullable()->constrained($tableNames['taxonomies']);
2126
$table->integer('sort_order')->default(0);
2227
$table->unsignedInteger('lft')->nullable()->index();
2328
$table->unsignedInteger('rgt')->nullable()->index();
@@ -32,9 +37,9 @@ public function up(): void
3237
$table->index(['type', 'lft', 'rgt']);
3338
});
3439

35-
Schema::create($tableNames['taxonomables'], function (Blueprint $table) use ($morphType) {
40+
Schema::create($tableNames['taxonomables'], function (Blueprint $table) use ($morphType, $tableNames) {
3641
$table->id();
37-
$table->foreignId('taxonomy_id')->constrained()->cascadeOnDelete();
42+
$table->foreignId('taxonomy_id')->constrained($tableNames['taxonomies'])->cascadeOnDelete();
3843

3944
$name = 'taxonomable';
4045
switch ($morphType) {
@@ -55,6 +60,10 @@ public function up(): void
5560
public function down(): void
5661
{
5762
$tableNames = config('taxonomy.table_names');
63+
$tableNames = array_merge([
64+
'taxonomies' => 'taxonomies',
65+
'taxonomables' => 'taxonomables',
66+
], (array) config('taxonomy.table_names', []));
5867

5968
Schema::dropIfExists($tableNames['taxonomables']);
6069
Schema::dropIfExists($tableNames['taxonomies']);

0 commit comments

Comments
 (0)