@@ -27,6 +27,13 @@ class MigrationGenerator implements Generator
27
27
'no_action ' => "->onDelete('no action') " ,
28
28
];
29
29
30
+ const ON_UPDATE_CLAUSES = [
31
+ 'cascade ' => "->onUpdate('cascade') " ,
32
+ 'restrict ' => "->onUpdate('restrict') " ,
33
+ 'null ' => "->onUpdate('set null') " ,
34
+ 'no_action ' => "->onUpdate('no action') " ,
35
+ ];
36
+
30
37
const UNSIGNABLE_TYPES = [
31
38
'bigInteger ' ,
32
39
'decimal ' ,
@@ -196,6 +203,7 @@ protected function buildDefinition(Model $model)
196
203
$ modifiers = collect ($ modifiers )->reject (function ($ modifier ) {
197
204
return (is_array ($ modifier ) && key ($ modifier ) === 'foreign ' )
198
205
|| (is_array ($ modifier ) && key ($ modifier ) === 'onDelete ' )
206
+ || (is_array ($ modifier ) && key ($ modifier ) === 'onUpdate ' )
199
207
|| $ modifier === 'foreign '
200
208
|| ($ modifier === 'nullable ' && $ this ->isLaravel7orNewer ());
201
209
});
@@ -289,9 +297,18 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
289
297
$ table = Str::lower (Str::plural ($ attributes [0 ]));
290
298
}
291
299
300
+ $ on_delete_suffix = $ on_update_suffix = null ;
292
301
$ on_delete_clause = collect ($ modifiers )->firstWhere ('onDelete ' );
293
- $ on_delete_clause = $ on_delete_clause ? $ on_delete_clause ['onDelete ' ] : config ('blueprint.on_delete ' , 'cascade ' );
294
- $ on_delete_suffix = self ::ON_DELETE_CLAUSES [$ on_delete_clause ];
302
+ if (config ('blueprint.use_constraints ' ) || $ on_delete_clause ) {
303
+ $ on_delete_clause = $ on_delete_clause ? $ on_delete_clause ['onDelete ' ] : config ('blueprint.on_delete ' , 'cascade ' );
304
+ $ on_delete_suffix = self ::ON_DELETE_CLAUSES [$ on_delete_clause ];
305
+ }
306
+
307
+ $ on_update_clause = collect ($ modifiers )->firstWhere ('onUpdate ' );
308
+ if (config ('blueprint.use_constraints ' ) || $ on_update_clause ) {
309
+ $ on_update_clause = $ on_update_clause ? $ on_update_clause ['onUpdate ' ] : config ('blueprint.on_update ' , 'cascade ' );
310
+ $ on_update_suffix = self ::ON_UPDATE_CLAUSES [$ on_update_clause ];
311
+ }
295
312
296
313
if ($ this ->isLaravel7orNewer () && $ type === 'id ' ) {
297
314
$ prefix = in_array ('nullable ' , $ modifiers )
@@ -301,17 +318,21 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
301
318
if ($ on_delete_clause === 'cascade ' ) {
302
319
$ on_delete_suffix = '->cascadeOnDelete() ' ;
303
320
}
321
+ if ($ on_update_clause === 'cascade ' ) {
322
+ $ on_update_suffix = '->cascadeOnUpdate() ' ;
323
+ }
324
+
304
325
if ($ column_name === Str::singular ($ table ).'_ ' .$ column ) {
305
- return self ::INDENT ."{$ prefix }->constrained() {$ on_delete_suffix }" ;
326
+ return self ::INDENT ."{$ prefix }->constrained() {$ on_delete_suffix }{ $ on_update_suffix } " ;
306
327
}
307
328
if ($ column === 'id ' ) {
308
- return self ::INDENT ."{$ prefix }->constrained(' {$ table }') {$ on_delete_suffix }" ;
329
+ return self ::INDENT ."{$ prefix }->constrained(' {$ table }') {$ on_delete_suffix }{ $ on_update_suffix } " ;
309
330
}
310
331
311
- return self ::INDENT ."{$ prefix }->constrained(' {$ table }', ' {$ column }') {$ on_delete_suffix }" ;
332
+ return self ::INDENT ."{$ prefix }->constrained(' {$ table }', ' {$ column }') {$ on_delete_suffix }{ $ on_update_suffix } " ;
312
333
}
313
334
314
- return self ::INDENT .'$table->foreign ' ."(' {$ column_name }')->references(' {$ column }')->on(' {$ table }') {$ on_delete_suffix }" ;
335
+ return self ::INDENT .'$table->foreign ' ."(' {$ column_name }')->references(' {$ column }')->on(' {$ table }') {$ on_delete_suffix }{ $ on_update_suffix } " ;
315
336
}
316
337
317
338
protected function disableForeignKeyConstraints ($ stub ): string
0 commit comments