Skip to content

Commit de0233f

Browse files
committed
wip
1 parent 7309502 commit de0233f

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
build
22
composer.lock
3-
docs
43
vendor
54
coverage
65
.idea

docs/_index.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
packageName: Laravel Query Detector
3+
githubUrl: https://github.com/beyondcode/laravel-query-detector
4+
---

docs/installation.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Installation
3+
order: 1
4+
---
5+
# Laravel N+1 Query Detector
6+
7+
The Laravel N+1 query detector helps you to increase your application's performance by reducing the number of queries it executes. This package monitors your queries in real-time, while you develop your application and notify you when you should add eager loading (N+1 queries).
8+
9+
# Installation
10+
11+
You can install the package via composer:
12+
13+
```
14+
composer require beyondcode/laravel-query-detector --dev
15+
```
16+
17+
The package will automatically register itself.

docs/usage.md

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: Usage
3+
order: 2
4+
---
5+
6+
## Usage
7+
8+
If you run your application in the `debug` mode, the query monitor will be automatically active. So there is nothing you have to do.
9+
10+
By default, this package will display an `alert()` message to notify you about an N+1 query found in the current request.
11+
12+
If you rather want this information to be written to your `laravel.log` file, written to your browser's console log as a warning or listed in a new tab for the [Laravel Debugbar (barryvdh/laravel-debugbar)](https://github.com/barryvdh/laravel-debugbar), you can publish the configuration and change the output behaviour (see example below).
13+
14+
You can publish the package's configuration using this command:
15+
16+
```bash
17+
php artisan vendor:publish --provider="BeyondCode\QueryDetector\QueryDetectorServiceProvider"
18+
```
19+
20+
This will add the `querydetector.php` file in your config directory with the following contents:
21+
22+
```php
23+
return [
24+
/*
25+
* Enable or disable the query detection.
26+
* If this is set to "null", the app.debug config value will be used.
27+
*/
28+
'enabled' => env('QUERY_DETECTOR_ENABLED', null),
29+
30+
/*
31+
* Threshold level for the N+1 query detection. If a relation query will be
32+
* executed more then this amount, the detector will notify you about it.
33+
*/
34+
'threshold' => (int) env('QUERY_DETECTOR_THRESHOLD', 1),
35+
36+
/*
37+
* Here you can whitelist model relations.
38+
*
39+
* Right now, you need to define the model relation both as the class name and the attribute name on the model.
40+
* So if an "Author" model would have a "posts" relation that points to a "Post" class, you need to add both
41+
* the "posts" attribute and the "Post::class", since the relation can get resolved in multiple ways.
42+
*/
43+
'except' => [
44+
//Author::class => [
45+
// Post::class,
46+
// 'posts',
47+
//]
48+
],
49+
50+
/*
51+
* Define the output format that you want to use. Multiple classes are supported.
52+
* Available options are:
53+
*
54+
* Alert:
55+
* Displays an alert on the website
56+
* \BeyondCode\QueryDetector\Outputs\Alert::class
57+
*
58+
* Console:
59+
* Writes the N+1 queries into your browsers console log
60+
* \BeyondCode\QueryDetector\Outputs\Console::class
61+
*
62+
* Clockwork: (make sure you have the itsgoingd/clockwork package installed)
63+
* Writes the N+1 queries warnings to Clockwork log
64+
* \BeyondCode\QueryDetector\Outputs\Clockwork::class
65+
*
66+
* Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
67+
* Writes the N+1 queries into a custom messages collector of Debugbar
68+
* \BeyondCode\QueryDetector\Outputs\Debugbar::class
69+
*
70+
* JSON:
71+
* Writes the N+1 queries into the response body of your JSON responses
72+
* \BeyondCode\QueryDetector\Outputs\Json::class
73+
*
74+
* Log:
75+
* Writes the N+1 queries into the Laravel.log file
76+
* \BeyondCode\QueryDetector\Outputs\Log::class
77+
*/
78+
'output' => [
79+
\BeyondCode\QueryDetector\Outputs\Log::class,
80+
\BeyondCode\QueryDetector\Outputs\Alert::class,
81+
]
82+
83+
];
84+
```
85+
86+
If you use **Lumen**, you need to copy the config file manually and register the Lumen Service Provider in `bootstrap/app.php` file
87+
88+
```php
89+
$app->register(\BeyondCode\QueryDetector\LumenQueryDetectorServiceProvider::class);
90+
```
91+
92+
If you need additional logic to run when the package detects unoptimized queries, you can listen to the `\BeyondCode\QueryDetector\Events\QueryDetected` event and write a listener to run your own handler. (e.g. send warning to Sentry/Bugsnag, send Slack notification, etc.)

0 commit comments

Comments
 (0)