Laravel: Automatically Log N+1 Queries to Bugsnag or Similar Tool

We have all seen cases where our performance was slow due to a random N+1 query. How to detect them in a LIVE application, when it's already out? And, even better, how to automatically log such cases in a tool like Bugsnag? Let me show you.

N+1 query is one of the most common performance mistakes, we have a lot of content about it. Luckily, Laravel has a built-in solution for it:

app/Providers/AppServiceProvider.php

use Illuminate\Database\Eloquent\Model;
 
public function boot()
{
Model::preventLazyLoading();
}

With this single line of code, you can throw exceptions if you ever have an N+1 query in your application.

But the official Laravel documentation recommends using it only on your local environment:

Model::preventLazyLoading(! $this->app->isProduction());

It's logical: you don't want your real app users to see the page totally failing with the exception just because it's a bit slower, right?

But what if you do want to detect N+1 queries on a live website, just silently log these exceptions, without showing an error page? Well, it's quite easy to do so, using the method handleLazyLoadingViolationUsing().

For this example, let's use a bug logging tool called Bugsnag, but similar logic could be for Sentry, Rollbar, Flare, and others.

Notice: we are skipping the Bugsnag setup part as their documentation is really well-made!

app/Providers/AppServiceProvider.php

use Illuminate\Database\Eloquent\Model;
 
public function boot()
{
Model::preventLazyLoading();
Model::handleLazyLoadingViolationUsing(function ($model, $relation) {
Bugsnag::notifyError("N+1 Query detected", sprintf("N+1 Query detected in %s::%s", get_class($model), $relation));
});
}

Now, whenever you have an N+1 query, you will get a notification in Bugsnag. You can also use this to log the N+1 query in your log files, or even send a notification to Slack. Here's how it looks in Bugsnag:

Or in the case of a Controller:

That's it - now all your N+1 queries will be logged in Bugsnag! And you can even add more channels as needed.

avatar

This is nice, thanks for sharing

👍 2

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 58 courses (1056 lessons, total 44 h 09 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials