Prevent Silently Discarding Attributes
- Laravel
- Eloquent
Similar to preventAccessingMissingAttributes, Laravel provides a preventSilentlyDiscardingAttributes method that can help prevent unexpected behaviour when updating models.
1namespace App\Providers; 2 3use Illuminate\Database\Eloquent\Model; 4use Illuminate\Support\ServiceProvider; 5 6class AppServiceProvider extends ServiceProvider 7{ 8 public function boot(): void 9 {10 Model::preventSilentlyDiscardingAttributes();11 }12}
If you tried to update an attribute on a model that was not fillable, you would get this error:
1Add fillable property [email_verified_at] to allow mass assignment on [App\Models\User].