Skip to content

Commit e146f0e

Browse files
committed
Fix issues with Debugbar output when used with SPAs/ajax requests
Debugbar's UI only shows tabs for MessageCollectors added during the initial page load. Previously, the N+1 Queries MessageCollector was only added to Debugbar when N+1 queries were actually detected. This meant that if no queries were detected on the initial page load, but they were detected during subsequent requests made via JS, those queries wouldn't show up because the tab wouldn't appear within Debugbar.
1 parent 488797e commit e146f0e

File tree

7 files changed

+45
-9
lines changed

7 files changed

+45
-9
lines changed

src/Outputs/Alert.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class Alert implements Output
99
{
10+
public function boot()
11+
{
12+
//
13+
}
14+
1015
public function output(Collection $detectedQueries, Response $response)
1116
{
1217
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {

src/Outputs/Console.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class Console implements Output
99
{
10+
public function boot()
11+
{
12+
//
13+
}
14+
1015
public function output(Collection $detectedQueries, Response $response)
1116
{
1217
if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) {

src/Outputs/Debugbar.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@
1010

1111
class Debugbar implements Output
1212
{
13-
public function output(Collection $detectedQueries, Response $response)
13+
protected $collector;
14+
15+
public function boot()
1416
{
15-
$collector = new MessagesCollector('N+1 Queries');
17+
$this->collector = new MessagesCollector('N+1 Queries');
18+
19+
LaravelDebugbar::addCollector($this->collector);
20+
}
1621

22+
public function output(Collection $detectedQueries, Response $response)
23+
{
1724
foreach ($detectedQueries as $detectedQuery) {
18-
$collector->addMessage(sprintf('Model: %s => Relation: %s - You should add `with(%s)` to eager-load this relation.',
25+
$this->collector->addMessage(sprintf('Model: %s => Relation: %s - You should add `with(%s)` to eager-load this relation.',
1926
$detectedQuery['model'],
2027
$detectedQuery['relation'],
2128
$detectedQuery['relation']
2229
));
2330
}
24-
25-
LaravelDebugbar::addCollector($collector);
2631
}
2732
}

src/Outputs/Json.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
class Json implements Output
99
{
10+
public function boot()
11+
{
12+
//
13+
}
1014

1115
public function output(Collection $detectedQueries, Response $response)
1216
{

src/Outputs/Log.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class Log implements Output
1010
{
11+
public function boot()
12+
{
13+
//
14+
}
15+
1116
public function output(Collection $detectedQueries, Response $response)
1217
{
1318
LaravelLog::info('Detected N+1 Query');
@@ -23,4 +28,4 @@ public function output(Collection $detectedQueries, Response $response)
2328
}
2429
}
2530
}
26-
}
31+
}

src/Outputs/Output.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77

88
interface Output
99
{
10+
public function boot();
11+
1012
public function output(Collection $detectedQueries, Response $response);
11-
}
13+
}

src/QueryDetector.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public function boot()
2626

2727
$this->logQuery($query, $backtrace);
2828
});
29+
30+
foreach ($this->getOutputTypes() as $outputType) {
31+
app()->singleton($outputType);
32+
app($outputType)->boot();
33+
}
2934
}
3035

3136
public function isEnabled(): bool
@@ -179,15 +184,20 @@ public function getDetectedQueries(): Collection
179184
return $queries;
180185
}
181186

182-
protected function applyOutput(Response $response)
187+
protected function getOutputTypes()
183188
{
184189
$outputTypes = config('querydetector.output');
185190

186191
if (! is_array($outputTypes)) {
187192
$outputTypes = [$outputTypes];
188193
}
189194

190-
foreach ($outputTypes as $type) {
195+
return $outputTypes;
196+
}
197+
198+
protected function applyOutput(Response $response)
199+
{
200+
foreach ($this->getOutputTypes() as $type) {
191201
app($type)->output($this->getDetectedQueries(), $response);
192202
}
193203
}

0 commit comments

Comments
 (0)