@@ -181,13 +181,27 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
181
181
*/
182
182
protected static $ modelsShouldPreventSilentlyDiscardingAttributes = false ;
183
183
184
+ /**
185
+ * The callback that is responsible for handling discarded attribute violations.
186
+ *
187
+ * @var callable|null
188
+ */
189
+ protected static $ discardedAttributeViolationCallback ;
190
+
184
191
/**
185
192
* Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.
186
193
*
187
194
* @var bool
188
195
*/
189
196
protected static $ modelsShouldPreventAccessingMissingAttributes = false ;
190
197
198
+ /**
199
+ * The callback that is responsible for handling missing attribute violations.
200
+ *
201
+ * @var callable|null
202
+ */
203
+ protected static $ missingAttributeViolationCallback ;
204
+
191
205
/**
192
206
* Indicates if broadcasting is currently enabled.
193
207
*
@@ -430,6 +444,17 @@ public static function preventSilentlyDiscardingAttributes($value = true)
430
444
static ::$ modelsShouldPreventSilentlyDiscardingAttributes = $ value ;
431
445
}
432
446
447
+ /**
448
+ * Register a callback that is responsible for handling discarded attribute violations.
449
+ *
450
+ * @param callable|null $callback
451
+ * @return void
452
+ */
453
+ public static function handleDiscardedAttributeViolationUsing (?callable $ callback )
454
+ {
455
+ static ::$ discardedAttributeViolationCallback = $ callback ;
456
+ }
457
+
433
458
/**
434
459
* Prevent accessing missing attributes on retrieved models.
435
460
*
@@ -441,6 +466,17 @@ public static function preventAccessingMissingAttributes($value = true)
441
466
static ::$ modelsShouldPreventAccessingMissingAttributes = $ value ;
442
467
}
443
468
469
+ /**
470
+ * Register a callback that is responsible for handling lazy loading violations.
471
+ *
472
+ * @param callable|null $callback
473
+ * @return void
474
+ */
475
+ public static function handleMissingAttributeViolationUsing (?callable $ callback )
476
+ {
477
+ static ::$ missingAttributeViolationCallback = $ callback ;
478
+ }
479
+
444
480
/**
445
481
* Execute a callback without broadcasting any model events for all model types.
446
482
*
@@ -481,20 +517,30 @@ public function fill(array $attributes)
481
517
if ($ this ->isFillable ($ key )) {
482
518
$ this ->setAttribute ($ key , $ value );
483
519
} elseif ($ totallyGuarded || static ::preventsSilentlyDiscardingAttributes ()) {
484
- throw new MassAssignmentException (sprintf (
485
- 'Add [%s] to fillable property to allow mass assignment on [%s]. ' ,
486
- $ key , get_class ($ this )
487
- ));
520
+ if (isset (static ::$ discardedAttributeViolationCallback )) {
521
+ call_user_func (static ::$ discardedAttributeViolationCallback , $ this , [$ key ]);
522
+ } else {
523
+ throw new MassAssignmentException (sprintf (
524
+ 'Add [%s] to fillable property to allow mass assignment on [%s]. ' ,
525
+ $ key , get_class ($ this )
526
+ ));
527
+ }
488
528
}
489
529
}
490
530
491
531
if (count ($ attributes ) !== count ($ fillable ) &&
492
532
static ::preventsSilentlyDiscardingAttributes ()) {
493
- throw new MassAssignmentException (sprintf (
494
- 'Add fillable property [%s] to allow mass assignment on [%s]. ' ,
495
- implode (', ' , array_diff (array_keys ($ attributes ), array_keys ($ fillable ))),
496
- get_class ($ this )
497
- ));
533
+ $ keys = array_diff (array_keys ($ attributes ), array_keys ($ fillable ));
534
+
535
+ if (isset (static ::$ discardedAttributeViolationCallback )) {
536
+ call_user_func (static ::$ discardedAttributeViolationCallback , $ this , $ keys );
537
+ } else {
538
+ throw new MassAssignmentException (sprintf (
539
+ 'Add fillable property [%s] to allow mass assignment on [%s]. ' ,
540
+ implode (', ' , $ keys ),
541
+ get_class ($ this )
542
+ ));
543
+ }
498
544
}
499
545
500
546
return $ this ;
@@ -1025,7 +1071,7 @@ public function push()
1025
1071
// us to recurse into all of these nested relations for the model instance.
1026
1072
foreach ($ this ->relations as $ models ) {
1027
1073
$ models = $ models instanceof Collection
1028
- ? $ models ->all () : [$ models ];
1074
+ ? $ models ->all () : [$ models ];
1029
1075
1030
1076
foreach (array_filter ($ models ) as $ model ) {
1031
1077
if (! $ model ->push ()) {
@@ -1072,7 +1118,7 @@ public function save(array $options = [])
1072
1118
// clause to only update this model. Otherwise, we'll just insert them.
1073
1119
if ($ this ->exists ) {
1074
1120
$ saved = $ this ->isDirty () ?
1075
- $ this ->performUpdate ($ query ) : true ;
1121
+ $ this ->performUpdate ($ query ) : true ;
1076
1122
}
1077
1123
1078
1124
// If the model is brand new, we'll insert it into our database and set the
@@ -1474,8 +1520,8 @@ public function registerGlobalScopes($builder)
1474
1520
public function newQueryWithoutScopes ()
1475
1521
{
1476
1522
return $ this ->newModelQuery ()
1477
- ->with ($ this ->with )
1478
- ->withCount ($ this ->withCount );
1523
+ ->with ($ this ->with )
1524
+ ->withCount ($ this ->withCount );
1479
1525
}
1480
1526
1481
1527
/**
@@ -1498,8 +1544,8 @@ public function newQueryWithoutScope($scope)
1498
1544
public function newQueryForRestoration ($ ids )
1499
1545
{
1500
1546
return is_array ($ ids )
1501
- ? $ this ->newQueryWithoutScopes ()->whereIn ($ this ->getQualifiedKeyName (), $ ids )
1502
- : $ this ->newQueryWithoutScopes ()->whereKey ($ ids );
1547
+ ? $ this ->newQueryWithoutScopes ()->whereIn ($ this ->getQualifiedKeyName (), $ ids )
1548
+ : $ this ->newQueryWithoutScopes ()->whereKey ($ ids );
1503
1549
}
1504
1550
1505
1551
/**
@@ -1547,7 +1593,7 @@ public function newCollection(array $models = [])
1547
1593
public function newPivot (self $ parent , array $ attributes , $ table , $ exists , $ using = null )
1548
1594
{
1549
1595
return $ using ? $ using ::fromRawAttributes ($ parent , $ attributes , $ table , $ exists )
1550
- : Pivot::fromAttributes ($ parent , $ attributes , $ table , $ exists );
1596
+ : Pivot::fromAttributes ($ parent , $ attributes , $ table , $ exists );
1551
1597
}
1552
1598
1553
1599
/**
@@ -1625,9 +1671,9 @@ public function fresh($with = [])
1625
1671
}
1626
1672
1627
1673
return $ this ->setKeysForSelectQuery ($ this ->newQueryWithoutScopes ())
1628
- ->useWritePdo ()
1629
- ->with (is_string ($ with ) ? func_get_args () : $ with )
1630
- ->first ();
1674
+ ->useWritePdo ()
1675
+ ->with (is_string ($ with ) ? func_get_args () : $ with )
1676
+ ->first ();
1631
1677
}
1632
1678
1633
1679
/**
@@ -1705,9 +1751,9 @@ public function replicateQuietly(array $except = null)
1705
1751
public function is ($ model )
1706
1752
{
1707
1753
return ! is_null ($ model ) &&
1708
- $ this ->getKey () === $ model ->getKey () &&
1709
- $ this ->getTable () === $ model ->getTable () &&
1710
- $ this ->getConnectionName () === $ model ->getConnectionName ();
1754
+ $ this ->getKey () === $ model ->getKey () &&
1755
+ $ this ->getTable () === $ model ->getTable () &&
1756
+ $ this ->getConnectionName () === $ model ->getConnectionName ();
1711
1757
}
1712
1758
1713
1759
/**
@@ -2050,8 +2096,8 @@ protected function resolveChildRouteBindingQuery($childType, $value, $field)
2050
2096
}
2051
2097
2052
2098
return $ relationship instanceof Model
2053
- ? $ relationship ->resolveRouteBindingQuery ($ relationship , $ value , $ field )
2054
- : $ relationship ->getRelated ()->resolveRouteBindingQuery ($ relationship , $ value , $ field );
2099
+ ? $ relationship ->resolveRouteBindingQuery ($ relationship , $ value , $ field )
2100
+ : $ relationship ->getRelated ()->resolveRouteBindingQuery ($ relationship , $ value , $ field );
2055
2101
}
2056
2102
2057
2103
/**
@@ -2295,8 +2341,8 @@ public static function __callStatic($method, $parameters)
2295
2341
public function __toString ()
2296
2342
{
2297
2343
return $ this ->escapeWhenCastingToString
2298
- ? e ($ this ->toJson ())
2299
- : $ this ->toJson ();
2344
+ ? e ($ this ->toJson ())
2345
+ : $ this ->toJson ();
2300
2346
}
2301
2347
2302
2348
/**
0 commit comments