Laravel Query Detector

News

July 16th, 2018

laravle-xray.jpg

Laravel Query Detector is a package by Marcel Pociot, which detects N+1 queries. The package will automatically notify you of any N+1 issues when your application is in debug mode.

We all run into the N+1 problem once in a while.
Our latest @laravelphp package is here to help you discover those N+1 queries and improve your application performance – simply by browsing your application.https://t.co/DMxEQVJa1B pic.twitter.com/6i3HLS8XZ7

— Marcel Pociot (@marcelpociot) July 15, 2018

The package monitors your queries in real-time while you develop your applications and notifies you of issues via a JavaScript alert(). You can also modify the package to send alerts to your Laravel log.

Here’s the default configuration that you can publish and override in your app:

1<?php
2
3return [
4 /*
5 * Enable or disable the query detection.
6 * If this is set to "null", the app.debug config value will be used.
7 */
8 'enabled' => env('QUERY_DETECTOR_ENABLED', null),
9
10 /*
11 * Threshold level for the N+1 query detection. If a relation query will be
12 * executed more then this amount, the detector will notify you about it.
13 */
14 'threshold' => 1,
15
16 /*
17 * Here you can whitelist model relations.
18 *
19 * Right now, you need to define the model relation both as the class name and the attribute name on the model.
20 * So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both
21 * the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways.
22 */
23 'except' => [
24 //Author::class => [
25 // Post::class,
26 // 'posts',
27 //]
28 ],
29
30 /*
31 * Define the output format that you want to use.
32 * Available options are:
33 *
34 * Alert:
35 * Displays an alert on the website
36 * \BeyondCode\QueryDetector\Outputs\Alert::class
37 *
38 * Log:
39 * Writes the N+1 queries into the Laravel.log file
40 * \BeyondCode\QueryDetector\Outputs\Log::class
41 */
42 'output' => \BeyondCode\QueryDetector\Outputs\Alert::class,
43
44];

So for example, if you want to update the configuration to send N+1 alerts to the log, you can update the configuration to the following:

1'output' => \BeyondCode\QueryDetector\Outputs\Log::class

You can install this package via composer with:

1composer require beyondcode/laravel-query-detector --dev

I’d like to note that this package configuration may change in the future and it looks like the release is a pre-release at the time of writing.

Learn More

You can learn more about this package and view the source code on the GitHub repository.

If you need a refresher or are not familiar with N+1, I wrote Optimize Laravel Eloquent Queries with Eager Loading which explains an overview of N+1, how eager loading can optimize your application, and a hands-on example demonstrating an N+1 query and updating it with eager loading.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.