@@ -131,15 +131,24 @@ protected function buildDefinition(Model $model)
131
131
$ foreign_modifier = $ column ->isForeignKey ();
132
132
133
133
if ($ this ->shouldAddForeignKeyConstraint ($ column )) {
134
- $ foreign = $ this ->buildForeignKey ($ column ->name (), $ foreign_modifier === 'foreign ' ? null : $ foreign_modifier , $ column ->dataType (), $ column ->attributes ());
134
+ $ foreign = $ this ->buildForeignKey (
135
+ $ column ->name (),
136
+ $ foreign_modifier === 'foreign ' ? null : $ foreign_modifier ,
137
+ $ column ->dataType (),
138
+ $ column ->attributes (),
139
+ $ column ->modifiers ()
140
+ );
141
+
135
142
if ($ column ->dataType () === 'id ' && $ this ->isLaravel7orNewer ()) {
136
143
$ column_definition = $ foreign ;
137
144
$ foreign = '' ;
138
145
}
139
146
140
147
// TODO: unset the proper modifier
141
148
$ modifiers = collect ($ modifiers )->reject (function ($ modifier ) {
142
- return (is_array ($ modifier ) && key ($ modifier ) === 'foreign ' ) || $ modifier === 'foreign ' ;
149
+ return ((is_array ($ modifier ) && key ($ modifier ) === 'foreign ' )
150
+ || $ modifier === 'foreign '
151
+ || ($ modifier === 'nullable ' && $ this ->isLaravel7orNewer ()));
143
152
});
144
153
}
145
154
@@ -198,7 +207,7 @@ protected function buildPivotTableDefinition(array $segments)
198
207
return trim ($ definition );
199
208
}
200
209
201
- protected function buildForeignKey (string $ column_name , ?string $ on , string $ type , array $ attributes = [])
210
+ protected function buildForeignKey (string $ column_name , ?string $ on , string $ type , array $ attributes = [], array $ modifiers = [] )
202
211
{
203
212
if (is_null ($ on )) {
204
213
$ table = Str::plural (Str::beforeLast ($ column_name , '_ ' ));
@@ -216,14 +225,18 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ
216
225
}
217
226
218
227
if ($ this ->isLaravel7orNewer () && $ type === 'id ' ) {
228
+ $ prefix = in_array ('nullable ' , $ modifiers )
229
+ ? '$table->foreignId ' . "(' {$ column_name }')->nullable() "
230
+ : '$table->foreignId ' . "(' {$ column_name }') " ;
231
+
219
232
if ($ column_name === Str::singular ($ table ) . '_ ' . $ column ) {
220
- return self ::INDENT . ' $table->foreignId ' . " (' { $ column_name } ') ->constrained()->cascadeOnDelete() " ;
233
+ return self ::INDENT . "{ $ prefix } ->constrained()->cascadeOnDelete() " ;
221
234
}
222
235
if ($ column === 'id ' ) {
223
- return self ::INDENT . ' $table->foreignId ' . " (' { $ column_name } ') ->constrained(' {$ table }')->cascadeOnDelete() " ;
236
+ return self ::INDENT . "{ $ prefix } ->constrained(' {$ table }')->cascadeOnDelete() " ;
224
237
}
225
238
226
- return self ::INDENT . ' $table->foreignId ' . " (' { $ column_name } ') ->constrained(' {$ table }', ' {$ column }')->cascadeOnDelete() " ;
239
+ return self ::INDENT . "{ $ prefix } ->constrained(' {$ table }', ' {$ column }')->cascadeOnDelete() " ;
227
240
}
228
241
229
242
return self ::INDENT . '$table->foreign ' . "(' {$ column_name }')->references(' {$ column }')->on(' {$ table }')->onDelete('cascade') " ;
0 commit comments