Eloquent
If you want to enable Sentry's Laravel SDK to capture Eloquent model violations, you can set up your integration in a way where Eloquent throws exceptions only when certain conditions are met. In most cases you wouldn't want these exceptions to be thrown in production, but you might still want to capture them in Sentry so you're aware that they're happening.
When you enable any of the Model::handle*ViolationUsing
methods as described below, Eloquent will no longer throw exceptions and the violations 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:
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 send them to Sentry after the request is finished. You can configure this behavior like this:
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
)
);
Read more about preventing lazy loading in the Laravel documentation.
To capture lazy loading violations add the following code to the boot
method of your AppServiceProvider
:
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()
);
}
Read more about preventing silently discarding attributes in the Laravel documentation.
To capture silently discarded attribute violations add the following code to the boot
method of your AppServiceProvider
:
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 add the following code to the boot
method of your AppServiceProvider
:
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()
);
}
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").
- Package:
- composer:sentry/sentry-laravel
- Version:
- 4.5.1
- Repository:
- https://github.com/getsentry/sentry-laravel