Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here
Tutorial Free

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

May 10, 2023
2 min read

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.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

JB
Joven Barola ✓ Link copied!

This is nice, thanks for sharing

D
DaveB ✓ Link copied!

Thank you Povilas(?). I just today gave up on BugSnag as I could not get it to work with Laravel 11 projects. Mostly I just get errors about the Bugsnag class couldn't be found. I find Sentry just works.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.