supliu/laravel-query-monitor
Installation:
composer require --dev supliu/laravel-query-monitor
php artisan vendor:publish --provider="Supliu\LaravelQueryMonitor\ServiceProvider"
config/laravel-query-monitor.php file is published.First Run:
php artisan laravel-query-monitor
Key Files:
config/laravel-query-monitor.php (host/port customization).Supliu\LaravelQueryMonitor\ServiceProvider (registers the monitor).Debugging a Slow API Endpoint:
php artisan laravel-query-monitor).POST /api/orders).Development Cycle:
SELECT *).with() clauses, use whereHas()).Integration with Testing:
// In a test case, use the monitor to verify query count/structure.
$this->withoutExceptionHandling();
$this->get('/test-route');
// Check terminal output for expected queries.
--dev).Team Collaboration:
Filtering Queries:
'ignored_queries' => [
'prisma:migrate',
'db:seed',
],
Performance Benchmarking:
# Branch A
php artisan laravel-query-monitor --port=8081
# Branch B
php artisan laravel-query-monitor --port=8082
Event-Driven Monitoring:
// In EventServiceProvider
public function boot()
{
Event::listen('eloquent.created: App\Models\User', function ($user) {
// Monitor will capture this query automatically.
});
}
Custom Output Formatting:
QueryMonitor class (see app/Providers/QueryMonitorServiceProvider.php for overrides).Port Conflicts:
8081 may clash with other services.php artisan laravel-query-monitor --port=9090
Update config/laravel-query-monitor.php to match.Missing Bindings:
'show_bindings' => env('APP_ENV') !== 'production',
High Query Volume:
--limit=10 (if supported) or filter queries in config.Socket Connection Issues:
0.0.0.0:<port>).APP_URL (must be accessible from the monitor’s host).Eager Loading Pitfalls:
with() is misused:
// Bad: Still fires N+1 for comments.
$posts = Post::with('comments')->get();
foreach ($posts as $post) {
$post->comments->load('user'); // Extra query!
}
Query Not Showing?:
ignored_queries in config).Slow Queries:
php artisan tinker
>>> \DB::enableQueryLog();
>>> $user = User::find(1);
>>> \DB::getQueryLog(); // Inspect manually.
Binding Values:
show_bindings in config to debug parameterized queries:
-- Before:
SELECT * FROM users WHERE email = ?
-- After:
SELECT * FROM users WHERE email = 'user@example.com'
Custom Query Formatting:
QueryMonitor class to highlight slow queries:
// app/Providers/QueryMonitorServiceProvider.php
public function register()
{
$this->app->bind('query-monitor', function () {
return new class extends \Supliu\LaravelQueryMonitor\QueryMonitor {
protected function formatQuery($query, $time)
{
$highlight = $time > 100 ? 'bgRed' : '';
return "<span class='$highlight'>$query</span> ($time ms)";
}
};
});
}
Log to File:
// In QueryMonitor class
public function logQuery($query, $time, $bindings)
{
file_put_contents(
storage_path('logs/query_monitor.log'),
"$query ($time ms)\n",
FILE_APPEND
);
}
Slack/Teams Notifications:
// In QueryMonitor class
public function handleQuery($query, $time)
{
if ($time > 500) {
$this->notifySlack($query, $time);
}
}
How can I help you explore Laravel packages today?