@@ -59,7 +59,7 @@ public function __construct(Filesystem $filesystem)
59
59
60
60
public function output (Tree $ tree , $ overwrite = false ): array
61
61
{
62
- $ tables = ['tableNames ' => [], 'pivotTableNames ' => []];
62
+ $ tables = ['tableNames ' => [], 'pivotTableNames ' => [], ' polymorphicManyToManyTables ' => [] ];
63
63
64
64
$ stub = $ this ->filesystem ->stub ('migration.stub ' );
65
65
/**
@@ -74,6 +74,12 @@ public function output(Tree $tree, $overwrite = false): array
74
74
$ tables ['pivotTableNames ' ][$ pivotTableName ] = $ this ->populatePivotStub ($ stub , $ pivotSegments );
75
75
}
76
76
}
77
+
78
+ if (!empty ($ model ->polymorphicManyToManyTables ())) {
79
+ foreach ($ model ->polymorphicManyToManyTables () as $ tableName ) {
80
+ $ tables ['polymorphicManyToManyTables ' ][Str::lower (Str::plural (Str::singular ($ tableName ).'able ' ))] = $ this ->populatePolyStub ($ stub , $ tableName );
81
+ }
82
+ }
77
83
}
78
84
79
85
return $ this ->createMigrations ($ tables , $ overwrite );
@@ -89,7 +95,7 @@ protected function createMigrations(array $tables, $overwrite = false): array
89
95
$ output = [];
90
96
91
97
$ sequential_timestamp = \Carbon \Carbon::now ()->copy ()->subSeconds (
92
- collect ($ tables ['tableNames ' ])->merge ($ tables ['pivotTableNames ' ])->count ()
98
+ collect ($ tables ['tableNames ' ])->merge ($ tables ['pivotTableNames ' ])->merge ( $ tables [ ' polymorphicManyToManyTables ' ])-> count ()
93
99
);
94
100
95
101
foreach ($ tables ['tableNames ' ] as $ tableName => $ data ) {
@@ -106,6 +112,14 @@ protected function createMigrations(array $tables, $overwrite = false): array
106
112
107
113
$ output [$ action ][] = $ path ;
108
114
}
115
+
116
+ foreach ($ tables ['polymorphicManyToManyTables ' ] as $ tableName => $ data ) {
117
+ $ path = $ this ->getTablePath ($ tableName , $ sequential_timestamp ->addSecond (), $ overwrite );
118
+ $ action = $ this ->filesystem ->exists ($ path ) ? 'updated ' : 'created ' ;
119
+ $ this ->filesystem ->put ($ path , $ data );
120
+ $ output [$ action ][] = $ path ;
121
+ }
122
+
109
123
return $ output ;
110
124
}
111
125
@@ -116,7 +130,7 @@ protected function populateStub(string $stub, Model $model)
116
130
$ stub = str_replace ('{{ definition }} ' , $ this ->buildDefinition ($ model ), $ stub );
117
131
118
132
if (Blueprint::useReturnTypeHints ()) {
119
- $ stub = str_replace (['up() ' ,'down() ' ], ['up(): void ' ,'down(): void ' ], $ stub );
133
+ $ stub = str_replace (['up() ' , 'down() ' ], ['up(): void ' , 'down(): void ' ], $ stub );
120
134
}
121
135
122
136
if ($ this ->hasForeignKeyConstraints ) {
@@ -139,6 +153,23 @@ protected function populatePivotStub(string $stub, array $segments)
139
153
return $ stub ;
140
154
}
141
155
156
+ protected function populatePolyStub (string $ stub , string $ parentTable )
157
+ {
158
+ $ stub = str_replace ('{{ class }} ' , $ this ->getPolyClassName ($ parentTable ), $ stub );
159
+ $ stub = str_replace ('{{ table }} ' , $ this ->getPolyTableName ($ parentTable ), $ stub );
160
+ $ stub = str_replace ('{{ definition }} ' , $ this ->buildPolyTableDefinition ($ parentTable ), $ stub );
161
+
162
+ if (Blueprint::useReturnTypeHints ()) {
163
+ $ stub = str_replace (['up() ' , 'down() ' ], ['up(): void ' , 'down(): void ' ], $ stub );
164
+ }
165
+
166
+ if ($ this ->hasForeignKeyConstraints ) {
167
+ $ stub = $ this ->disableForeignKeyConstraints ($ stub );
168
+ }
169
+
170
+ return $ stub ;
171
+ }
172
+
142
173
protected function buildDefinition (Model $ model )
143
174
{
144
175
$ definition = '' ;
@@ -286,6 +317,27 @@ protected function buildPivotTableDefinition(array $segments)
286
317
return trim ($ definition );
287
318
}
288
319
320
+ protected function buildPolyTableDefinition (string $ parentTable )
321
+ {
322
+ $ definition = '' ;
323
+
324
+ $ references = 'id ' ;
325
+ $ on = Str::lower (Str::plural ($ parentTable ));
326
+ $ foreign = Str::lower (Str::singular ($ parentTable )) . '_ ' . $ references ;
327
+
328
+ if (config ('blueprint.use_constraints ' )) {
329
+ $ this ->hasForeignKeyConstraints = true ;
330
+ $ definition .= $ this ->buildForeignKey ($ foreign , $ on , 'id ' ) . '; ' . PHP_EOL ;
331
+ } else {
332
+ $ definition .= self ::INDENT . '$table->foreignId( \'' . $ foreign . '\'); ' . PHP_EOL ;
333
+ }
334
+
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 ;
337
+
338
+ return trim ($ definition );
339
+ }
340
+
289
341
protected function buildForeignKey (string $ column_name , ?string $ on , string $ type , array $ attributes = [], array $ modifiers = [])
290
342
{
291
343
if (is_null ($ on )) {
@@ -409,6 +461,11 @@ protected function getPivotClassName(array $segments)
409
461
return 'Create ' . Str::studly ($ this ->getPivotTableName ($ segments )) . 'Table ' ;
410
462
}
411
463
464
+ protected function getPolyClassName (string $ parentTable )
465
+ {
466
+ return 'Create ' . Str::studly ($ this ->getPolyTableName ($ parentTable )) . 'Table ' ;
467
+ }
468
+
412
469
protected function getPivotTableName (array $ segments )
413
470
{
414
471
$ isCustom = collect ($ segments )
@@ -435,6 +492,11 @@ function ($name) {
435
492
return strtolower (implode ('_ ' , $ segments ));
436
493
}
437
494
495
+ protected function getPolyTableName (string $ parentTable )
496
+ {
497
+ return Str::plural (Str::lower (Str::singular ($ parentTable ) . 'able ' ));
498
+ }
499
+
438
500
private function shouldAddForeignKeyConstraint (\Blueprint \Models \Column $ column )
439
501
{
440
502
if ($ column ->name () === 'id ' ) {
0 commit comments