@@ -174,6 +174,13 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
174
174
*/
175
175
protected static $ lazyLoadingViolationCallback ;
176
176
177
+ /**
178
+ * Indicates if an exception should be thrown instead of silently discarding non-fillable attributes.
179
+ *
180
+ * @var bool
181
+ */
182
+ protected static $ modelsShouldPreventSilentlyDiscardingAttributes = false ;
183
+
177
184
/**
178
185
* Indicates if broadcasting is currently enabled.
179
186
*
@@ -392,6 +399,17 @@ public static function handleLazyLoadingViolationUsing(?callable $callback)
392
399
static ::$ lazyLoadingViolationCallback = $ callback ;
393
400
}
394
401
402
+ /**
403
+ * Prevent non-fillable attributes from being silently discarded.
404
+ *
405
+ * @param bool $value
406
+ * @return void
407
+ */
408
+ public static function preventSilentlyDiscardingAttributes ($ value = true )
409
+ {
410
+ static ::$ modelsShouldPreventSilentlyDiscardingAttributes = $ value ;
411
+ }
412
+
395
413
/**
396
414
* Execute a callback without broadcasting any model events for all model types.
397
415
*
@@ -429,7 +447,7 @@ public function fill(array $attributes)
429
447
// the model, and all others will just get ignored for security reasons.
430
448
if ($ this ->isFillable ($ key )) {
431
449
$ this ->setAttribute ($ key , $ value );
432
- } elseif ($ totallyGuarded ) {
450
+ } elseif ($ totallyGuarded || static :: preventsSilentlyDiscardingAttributes () ) {
433
451
throw new MassAssignmentException (sprintf (
434
452
'Add [%s] to fillable property to allow mass assignment on [%s]. ' ,
435
453
$ key , get_class ($ this )
@@ -2061,6 +2079,16 @@ public static function preventsLazyLoading()
2061
2079
return static ::$ modelsShouldPreventLazyLoading ;
2062
2080
}
2063
2081
2082
+ /**
2083
+ * Determine if discarding guarded attribute fills is disabled.
2084
+ *
2085
+ * @return bool
2086
+ */
2087
+ public static function preventsSilentlyDiscardingAttributes ()
2088
+ {
2089
+ return static ::$ modelsShouldPreventSilentlyDiscardingAttributes ;
2090
+ }
2091
+
2064
2092
/**
2065
2093
* Get the broadcast channel route definition that is associated with the given entity.
2066
2094
*
0 commit comments