Eloquent

Laravel has a few options to make Eloquent behave more "strict" and throw exceptions when certain conditions are met. Usually you would not want these exceptions to be thrown in production, but you might still want to capture them in Sentry so you know they are happening.

When you enable any of the Model::handle*ViolationUsing methods as described below it will not throw an exception anymore and it will only be reported to Sentry. If you also want to write to a log or do something else when a violation occurs you can pass a closure as the first argument to the Integration::*ViolationReporter method, for example:

Copied
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Sentry\Laravel\Integration;

Model::handleLazyLoadingViolationUsing(
    Integration::lazyLoadingViolationReporter(function ($model, $relation) {
        Log::warning('Lazy loading violation', ['model' => $model, 'relation' => $relation]);
    })
);

By default the reporters will suppress duplicate violations per request and will send them to Sentry after the request is finished. You can configure this behavior like this:

Copied
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::handleLazyLoadingViolationUsing(
    Integration::lazyLoadingViolationReporter(
        reportAfterResponse: true, // set to false to send the violation immediately
        suppressDuplicateReports: true, // set to false to send all violations, even if reported before in the same request
    )
);

To capture lazy loading violations you can add the following code to the boot method of your AppServiceProvider:

Copied
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventLazyLoading();

// Only supress lazy loading violations in production, let them be thrown in other environments
if (app()->isProduction()) {
  Model::handleLazyLoadingViolationUsing(
      Integration::lazyLoadingViolationReporter()
  );
}

To capture silently discarded attribute violations you can add the following code to the boot method of your AppServiceProvider:

Copied
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventSilentlyDiscardingAttributes();

// Only supress silently discarded attribute violations in production, let them be thrown in other environments
if (app()->isProduction()) {
  Model::handleDiscardedAttributeViolationUsing(
      Integration::discardedAttributeViolationReporter()
  );
}

To capture missing attribute violations you can add the following code to the boot method of your AppServiceProvider:

Copied
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventAccessingMissingAttributes();

// Only supress missing attribute violations in production, let them be thrown in other environments
if (app()->isProduction()) {
  Model::handleMissingAttributeViolationUsing(
      Integration::missingAttributeViolationReporter()
  );
}
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").