@@ -11,9 +11,10 @@ trait RequiredFields
11
11
/**
12
12
* Get only required fields for the model that
13
13
* need to be added while creating a new record in the database.
14
- * So, we ignore auto_increment, primary keys, nullable and default fields.
14
+ * So, we ignore auto_increment, primary keys, nullable
15
+ * and default fields (either from database or application).
15
16
*
16
- * @return array
17
+ * @return array<string>
17
18
*/
18
19
public static function getRequiredFields (
19
20
$ withNullables = false ,
@@ -28,6 +29,8 @@ public static function getRequiredFields(
28
29
);
29
30
}
30
31
32
+ $ modelDefaultAttributes = self ::getModelDefaultAttributes ();
33
+
31
34
$ primaryIndex = collect (Schema::getIndexes ((new self )->getTable ()))
32
35
->filter (function ($ index ) {
33
36
return $ index ['primary ' ];
@@ -50,6 +53,9 @@ public static function getRequiredFields(
50
53
$ column ['default ' ] != null && ! $ withDefaults ||
51
54
(in_array ($ column ['name ' ], $ primaryIndex ));
52
55
})
56
+ ->reject (function ($ column ) use ($ modelDefaultAttributes , $ withDefaults ) {
57
+ return in_array ($ column ['name ' ], $ modelDefaultAttributes ) && ! $ withDefaults ;
58
+ })
53
59
->pluck ('name ' )
54
60
->when ($ withPrimaryKey , function ($ collection ) use ($ primaryIndex ) {
55
61
return $ collection ->prepend (...$ primaryIndex );
@@ -105,6 +111,7 @@ protected static function getRequiredFieldsForSqlite(
105
111
$ withPrimaryKey = false
106
112
) {
107
113
$ table = self ::getTableFromThisModel ();
114
+ $ modelDefaultAttributes = self ::getModelDefaultAttributes ();
108
115
109
116
$ queryResult = DB ::select (/** @lang SQLite */ "PRAGMA table_info( $ table) " );
110
117
@@ -117,8 +124,12 @@ protected static function getRequiredFieldsForSqlite(
117
124
|| $ column ['dflt_value ' ] != null && ! $ withDefaults
118
125
|| ! $ column ['notnull ' ] && ! $ withNullables ;
119
126
})
127
+ ->reject (function ($ column ) use ($ modelDefaultAttributes , $ withDefaults ) {
128
+ return in_array ($ column ['name ' ], $ modelDefaultAttributes ) && ! $ withDefaults ;
129
+ })
120
130
->pluck ('name ' )
121
131
->toArray ();
132
+
122
133
}
123
134
124
135
protected static function getRequiredFieldsForMysqlAndMariaDb (
@@ -127,6 +138,7 @@ protected static function getRequiredFieldsForMysqlAndMariaDb(
127
138
$ withPrimaryKey = false
128
139
) {
129
140
$ table = self ::getTableFromThisModel ();
141
+ $ modelDefaultAttributes = self ::getModelDefaultAttributes ();
130
142
131
143
$ queryResult = DB ::select (
132
144
/** @lang SQLite */ "
@@ -162,6 +174,9 @@ protected static function getRequiredFieldsForMysqlAndMariaDb(
162
174
|| $ column ['default ' ] != null && ! $ withDefaults
163
175
|| $ column ['nullable ' ] && ! $ withNullables ;
164
176
})
177
+ ->reject (function ($ column ) use ($ modelDefaultAttributes , $ withDefaults ) {
178
+ return in_array ($ column ['name ' ], $ modelDefaultAttributes ) && ! $ withDefaults ;
179
+ })
165
180
->pluck ('name ' )
166
181
->toArray ();
167
182
}
@@ -172,6 +187,7 @@ protected static function getRequiredFieldsForPostgres(
172
187
$ withPrimaryKey = false
173
188
) {
174
189
$ table = self ::getTableFromThisModel ();
190
+ $ modelDefaultAttributes = self ::getModelDefaultAttributes ();
175
191
176
192
$ primaryIndex = DB ::select (/** @lang PostgreSQL */ "
177
193
SELECT
@@ -234,6 +250,9 @@ protected static function getRequiredFieldsForPostgres(
234
250
($ column ['nullable ' ] == 'YES ' && ! $ withNullables ) ||
235
251
(in_array ($ column ['name ' ], $ primaryIndex ));
236
252
})
253
+ ->reject (function ($ column ) use ($ modelDefaultAttributes , $ withDefaults ) {
254
+ return in_array ($ column ['name ' ], $ modelDefaultAttributes ) && ! $ withDefaults ;
255
+ })
237
256
->pluck ('name ' )
238
257
->when ($ withPrimaryKey , function ($ collection ) use ($ primaryIndex ) {
239
258
return $ collection ->prepend (...$ primaryIndex );
@@ -248,6 +267,7 @@ protected static function getRequiredFieldsForSqlServer(
248
267
$ withPrimaryKey = false
249
268
) {
250
269
$ table = self ::getTableFromThisModel ();
270
+ $ modelDefaultAttributes = self ::getModelDefaultAttributes ();
251
271
252
272
$ primaryIndex = DB ::select (/** @lang TSQL */ '
253
273
SELECT
@@ -295,6 +315,9 @@ protected static function getRequiredFieldsForSqlServer(
295
315
|| $ column ['nullable ' ] && ! $ withNullables
296
316
|| (in_array ($ column ['name ' ], $ primaryIndex ) && ! $ withPrimaryKey );
297
317
})
318
+ ->reject (function ($ column ) use ($ modelDefaultAttributes , $ withDefaults ) {
319
+ return in_array ($ column ['name ' ], $ modelDefaultAttributes ) && ! $ withDefaults ;
320
+ })
298
321
->pluck ('name ' )
299
322
->toArray ();
300
323
}
@@ -309,6 +332,14 @@ protected static function getTableFromThisModel()
309
332
return str_replace ('. ' , '__ ' , $ table );
310
333
}
311
334
335
+ /**
336
+ * @return array<string>
337
+ */
338
+ protected static function getModelDefaultAttributes ()
339
+ {
340
+ return array_keys ((new self )->getAttributes ());
341
+ }
342
+
312
343
public static function getRequiredFieldsWithNullables ()
313
344
{
314
345
return self ::getRequiredFields ($ withNullables = true , $ withDefaults = false , $ withPrimaryKey = false );
0 commit comments