Skip to content

Conversation

jeysonlesmes
Copy link

Integration with ajax requests to show the alert when an N+1 is detected


$validateContentTypeHTML = true;

if (env('QUERY_DETECTOR_AJAX', false)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use config(...), not env(...) because when config are cached env() will always return null.

Config reference:
https://laravel.com/docs/7.x/configuration#configuration-caching


return $output;
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line :(

$output .= "\\n";
if (!request()->ajax()) {
$output = '<script type="text/javascript">';
$output .= "alert('Found the following N+1 queries in this request:\\n\\n";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest another way.
Maybe blade template will look better?

$output .= "Model: ".addslashes($detectedQuery['model'])." => Relation: ".addslashes($detectedQuery['relation']);
$output .= " - You should add \"with(\'".addslashes($detectedQuery['relation'])."\')\" to eager-load this relation.";
$output .= "\\n";
if (!request()->ajax()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate check for request()->ajax()


``` javascript
$(document).ajaxComplete(function(event, response){
if (typeof response.responseJSON.laravelQueryDetector != 'undefined') {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (typeof response.responseJSON.laravelQueryDetector != 'undefined') {
if (typeof response.responseJSON.laravelQueryDetector !== 'undefined') {

@rvanbaalen
Copy link

What's the status of this PR? I'd love to use query-detector but most of my requests are async via javascript so right now it basically detects nothing.

@mechelon
Copy link
Member

mechelon commented Oct 4, 2024

I'll close this. Feel free to do create another PR with the reviewed suggestions and tests.

@mechelon mechelon closed this Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants