Skip to content

Commit d3763f5

Browse files
authored
Merge pull request #11 from crlcu/master
Debugbar output
2 parents b9a329a + 3633926 commit d3763f5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

config/config.php

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
* Displays an alert on the website
3030
* \BeyondCode\QueryDetector\Outputs\Alert::class
3131
*
32+
* Debugbar: (make sure you have the barryvdh/laravel-debugbar package installed)
33+
* Writes the N+1 queries into a custom messages collector of Debugbar
34+
* \BeyondCode\QueryDetector\Outputs\Debugbar::class
35+
*
3236
* Log:
3337
* Writes the N+1 queries into the Laravel.log file
3438
* \BeyondCode\QueryDetector\Outputs\Log::class

src/Outputs/Debugbar.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace BeyondCode\QueryDetector\Outputs;
4+
5+
use Illuminate\Support\Collection;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
use Debugbar as LaravelDebugbar;
9+
use DebugBar\DataCollector\MessagesCollector;
10+
11+
class Debugbar implements Output
12+
{
13+
public function output(Collection $detectedQueries, Response $response)
14+
{
15+
$collector = new MessagesCollector('N+1 Queries');
16+
17+
foreach ($detectedQueries as $detectedQuery) {
18+
$collector->addMessage(sprintf('Model: %s => Relation: %s - You should add with(%s) to eager-load this relation.',
19+
$detectedQuery['model'],
20+
$detectedQuery['relation'],
21+
$detectedQuery['relation']
22+
));
23+
}
24+
25+
LaravelDebugbar::addCollector($collector);
26+
}
27+
}

0 commit comments

Comments
 (0)