7
7
use Blueprint \Models \Model ;
8
8
use Blueprint \Tree ;
9
9
use Carbon \Carbon ;
10
- use Illuminate \Support \Facades \App ;
11
10
use Illuminate \Support \Str ;
12
11
use Symfony \Component \Finder \SplFileInfo ;
13
12
use Illuminate \Filesystem \Filesystem ;
@@ -173,7 +172,7 @@ protected function buildDefinition(Model $model)
173
172
$ column_definition .= '$table-> ' . $ dataType . "(' {$ column ->name ()}' " ;
174
173
}
175
174
176
- if (! empty ($ column ->attributes ()) && ! in_array ($ column ->dataType (), [ ' id ' , ' uuid ' ] )) {
175
+ if (! empty ($ column ->attributes ()) && ! $ this -> isIdOrUuid ($ column ->dataType ())) {
177
176
$ column_definition .= ', ' ;
178
177
if (in_array ($ column ->dataType (), ['set ' , 'enum ' ])) {
179
178
$ column_definition .= json_encode ($ column ->attributes ());
@@ -198,7 +197,7 @@ protected function buildDefinition(Model $model)
198
197
$ column ->modifiers ()
199
198
);
200
199
201
- if ($ column ->dataType () === ' id ' ) {
200
+ if ($ this -> isIdOrUuid ( $ column ->dataType ()) ) {
202
201
$ column_definition = $ foreign ;
203
202
$ foreign = '' ;
204
203
}
@@ -210,7 +209,7 @@ function ($modifier) use ($column) {
210
209
|| (is_array ($ modifier ) && key ($ modifier ) === 'onDelete ' )
211
210
|| (is_array ($ modifier ) && key ($ modifier ) === 'onUpdate ' )
212
211
|| $ modifier === 'foreign '
213
- || ($ modifier === 'nullable ' && $ column ->dataType () === ' id ' );
212
+ || ($ modifier === 'nullable ' && $ this -> isIdOrUuid ( $ column ->dataType ()) );
214
213
}
215
214
);
216
215
}
@@ -237,7 +236,6 @@ function ($modifier) use ($column) {
237
236
if (! empty ($ foreign )) {
238
237
$ column_definition .= $ foreign . '; ' . PHP_EOL ;
239
238
}
240
-
241
239
$ definition .= $ column_definition ;
242
240
}
243
241
@@ -301,7 +299,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
301
299
$ column = Str::afterLast ($ column_name , '_ ' );
302
300
}
303
301
304
- if ($ type === ' id ' && ! empty ($ attributes )) {
302
+ if ($ this -> isIdOrUuid ( $ type) && ! empty ($ attributes )) {
305
303
$ table = Str::lower (Str::plural ($ attributes [0 ]));
306
304
}
307
305
@@ -318,10 +316,16 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
318
316
$ on_update_suffix = self ::ON_UPDATE_CLAUSES [$ on_update_clause ];
319
317
}
320
318
321
- if ($ type === 'id ' ) {
319
+ if ($ this ->isIdOrUuid ($ type )) {
320
+ if ($ type === 'uuid ' ) {
321
+ $ method = 'foreignUuid ' ;
322
+ } else {
323
+ $ method = 'foreignId ' ;
324
+ }
325
+
322
326
$ prefix = in_array ('nullable ' , $ modifiers )
323
- ? '$table->foreignId ' . "(' {$ column_name }')->nullable() "
324
- : '$table->foreignId ' . "(' {$ column_name }') " ;
327
+ ? '$table-> ' . "{ $ method } (' {$ column_name }')->nullable() "
328
+ : '$table-> ' . "{ $ method } (' {$ column_name }') " ;
325
329
326
330
if ($ on_delete_clause === 'cascade ' ) {
327
331
$ on_delete_suffix = '->cascadeOnDelete() ' ;
@@ -439,7 +443,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column)
439
443
}
440
444
441
445
return config ('blueprint.use_constraints ' )
442
- && ($ column -> dataType () === ' id ' || $ column ->dataType () === ' uuid ' && Str::endsWith ($ column ->name (), '_id ' ));
446
+ && ($ this -> isIdOrUuid ( $ column ->dataType ()) && Str::endsWith ($ column ->name (), '_id ' ));
443
447
}
444
448
445
449
protected function isNumericDefault (string $ type , string $ value ): bool
@@ -459,4 +463,9 @@ function ($value) use ($type) {
459
463
}
460
464
);
461
465
}
466
+
467
+ protected function isIdOrUuid (string $ dataType )
468
+ {
469
+ return in_array ($ dataType , ['id ' , 'uuid ' ]);
470
+ }
462
471
}
0 commit comments