@@ -17,6 +17,13 @@ class MigrationGenerator implements Generator
17
17
'uuidMorphs ' ,
18
18
];
19
19
20
+ const ON_DELETE_CLAUSES = [
21
+ 'cascade ' => "->onDelete('cascade') " ,
22
+ 'restrict ' => "->onDelete('restrict') " ,
23
+ 'set_null ' => "->onDelete('set null') " ,
24
+ 'no_action ' => "->onDelete('no action') " ,
25
+ ];
26
+
20
27
const UNSIGNABLE_TYPES = [
21
28
'bigInteger ' ,
22
29
'decimal ' ,
@@ -148,7 +155,9 @@ protected function buildDefinition(Model $model)
148
155
149
156
// TODO: unset the proper modifier
150
157
$ modifiers = collect ($ modifiers )->reject (function ($ modifier ) {
151
- return ((is_array ($ modifier ) && key ($ modifier ) === 'foreign ' )
158
+ return (
159
+ (is_array ($ modifier ) && key ($ modifier ) === 'foreign ' )
160
+ || (is_array ($ modifier ) && key ($ modifier ) === 'onDelete ' )
152
161
|| $ modifier === 'foreign '
153
162
|| ($ modifier === 'nullable ' && $ this ->isLaravel7orNewer ()));
154
163
});
@@ -231,22 +240,29 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
231
240
$ table = Str::lower (Str::plural ($ attributes [0 ]));
232
241
}
233
242
243
+ $ on_delete_clause = config ('blueprint.on_delete ' , 'cascade ' );
244
+
245
+ $ on_delete_suffix = self ::ON_DELETE_CLAUSES [$ on_delete_clause ];
246
+
234
247
if ($ this ->isLaravel7orNewer () && $ type === 'id ' ) {
235
248
$ prefix = in_array ('nullable ' , $ modifiers )
236
249
? '$table->foreignId ' . "(' {$ column_name }')->nullable() "
237
250
: '$table->foreignId ' . "(' {$ column_name }') " ;
238
251
252
+ if ($ on_delete_clause === 'cascade ' ) {
253
+ $ on_delete_suffix = '->cascadeOnDelete() ' ;
254
+ }
239
255
if ($ column_name === Str::singular ($ table ) . '_ ' . $ column ) {
240
- return self ::INDENT . "{$ prefix }->constrained()->cascadeOnDelete() " ;
256
+ return self ::INDENT . "{$ prefix }->constrained() { $ on_delete_suffix } " ;
241
257
}
242
258
if ($ column === 'id ' ) {
243
- return self ::INDENT . "{$ prefix }->constrained(' {$ table }')->cascadeOnDelete() " ;
259
+ return self ::INDENT . "{$ prefix }->constrained(' {$ table }') { $ on_delete_suffix } " ;
244
260
}
245
261
246
- return self ::INDENT . "{$ prefix }->constrained(' {$ table }', ' {$ column }')->cascadeOnDelete() " ;
262
+ return self ::INDENT . "{$ prefix }->constrained(' {$ table }', ' {$ column }') { $ on_delete_suffix } " ;
247
263
}
248
264
249
- return self ::INDENT . '$table->foreign ' . "(' {$ column_name }')->references(' {$ column }')->on(' {$ table }')->onDelete('cascade') " ;
265
+ return self ::INDENT . '$table->foreign ' . "(' {$ column_name }')->references(' {$ column }')->on(' {$ table }') { $ on_delete_suffix } " ;
250
266
}
251
267
252
268
protected function getClassName (Model $ model )
0 commit comments