Overview Version History Q & A Rating & Review
Laravel N+1 Query Detector for VS Code
Overview
The Laravel N+1 Query Detector is a Visual Studio Code extension that helps identify potential N+1 query issues in Laravel PHP projects. It scans your PHP files and warns you about possible inefficient Eloquent queries inside loops.
Features
Automatically detects potential N+1 query issues inside loops.
Provides VS Code diagnostics with warnings.
Works with PHP files in Laravel projects.
Runs automatically on file open and modification.
Manually triggerable via a command.
Installation
Open VS Code.
Navigate to Extensions
(Ctrl+Shift+X).
Search for Laravel N+1 Query Detector
.
Click Install
.
Usage
Automatic Detection
The extension automatically scans all open PHP files and highlights potential N+1 query issues.
It updates diagnostics whenever a document is modified.
Manual Trigger
Open the Command Palette (Ctrl+Shift+P
or Cmd+Shift+P
on macOS).
Search for Detect Laravel N+1 Queries
.
Click to run the detection manually.
How It Works
The extension scans PHP files and detects foreach
loops.
It checks if an Eloquent query ($model->relation
, $model->method()
) is used inside the loop.
If found, a warning is displayed with a suggestion to use eager loading (with('relation')
).
Example of N+1 Query Issue
$users = User::all(); // Fetch all users
foreach ($users as $user) {
echo $user->posts; // This triggers an extra query for each user (N+1 issue)
}
License
This project is licensed under the MIT License .