---
title: "Eloquent"
description: "Learn how to enable Sentry's Laravel SDK to capture Eloquent model violations."
url: https://docs.sentry.io/platforms/php/guides/laravel/integrations/eloquent/
---

# Eloquent | Sentry for Laravel

## [Violation Reporters](https://docs.sentry.io/platforms/php/guides/laravel/integrations/eloquent.md#violation-reporters)

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:

```php
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:

```php
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
    )
);
```

### [Lazy Loading](https://docs.sentry.io/platforms/php/guides/laravel/integrations/eloquent.md#lazy-loading)

Read more about preventing lazy loading in the [Laravel documentation](https://laravel.com/docs/11.x/eloquent-relationships#preventing-lazy-loading).

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

```php
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventLazyLoading();

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

### [Silently Discarding Attributes (Mass Assignment Protection)](https://docs.sentry.io/platforms/php/guides/laravel/integrations/eloquent.md#silently-discarding-attributes-mass-assignment-protection)

Read more about preventing silently discarding attributes in the [Laravel documentation](https://laravel.com/docs/11.x/eloquent#configuring-eloquent-strictness).

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

```php
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventSilentlyDiscardingAttributes();

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

### [Missing Attributes](https://docs.sentry.io/platforms/php/guides/laravel/integrations/eloquent.md#missing-attributes)

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

```php
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;

Model::preventAccessingMissingAttributes();

// Only suppress missing attribute violations in production, let them be thrown in other environments
if (app()->isProduction()) {
  Model::handleMissingAttributeViolationUsing(
      Integration::missingAttributeViolationReporter()
  );
}
```
