Before the first feature, I add two guards to AppServiceProvider. They are small, boring, and they have saved me more debugging time than almost anything else in Laravel.

1. Prevent lazy loading

Model::preventLazyLoading(! $this->app->isProduction());
  • Lazy loading is prevented, so the N + 1 problem fails loudly in development.
  • Laravel throws when a model tries to lazy load a relationship.
  • You have to be explicit with with().

The production exception is deliberate. A missing eager load should fail in CI and local work, not take down a live page.

2. Prevent silently discarding attributes

Model::preventSilentlyDiscardingAttributes(! $this->app->isProduction());
  • Laravel no longer ignores attributes that do not exist on the model’s $fillable list or database table.
  • It throws when you set or retrieve an attribute that is not actually defined.
  • It catches typos, stale column names, and incorrect mass-assignment keys while you are still in development.

Why these two

Most Laravel bugs I have seen are not exotic. They are a relationship that loaded one query per row, or an attribute that quietly went nowhere. These guards turn both into immediate, readable errors.

Add them before the feature. Thank yourself later.

Thanks for reading.

Written by Jim Merioles / August 12, 2025