What is Eloquent lazy loading prevention?
Answer
Lazy loading prevention forces you to always eager-load relationships, turning the common N+1 problem into a detectable error during development. Enable in AppServiceProvider: Model::preventLazyLoading(!app()->isProduction()). When enabled, accessing a relationship that was not eager-loaded throws a LazyLoadingViolationException — forcing developers to add with() to their queries. In production, log the violation instead of throwing (by passing false). This is particularly valuable in team environments where developers may forget to eager-load and the performance problem only surfaces at scale. Related safety features: Model::preventSilentlyDiscardingAttributes() — throws when assigning a non-fillable attribute, and Model::preventAccessingMissingAttributes() — throws when accessing an attribute not in the model's attributes array.