Laravel DB Monitor is a real-time database performance monitoring package for Laravel applications.
It automatically detects slow queries, N+1 query problems, and missing indexes, then suggests SQL optimizations and can even generate index migrations for you.
A production-ready Laravel performance monitoring tool for Laravel 10, 11 & 12.
db:report)composer require benjdiasaad/laravel-db-monitor
Publish config & migrations:
php artisan vendor:publish --tag=db-monitor-config
php artisan vendor:publish --tag=db-monitor-migrations
php artisan migrate
bootstrap/app.php->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class,
]);
})
app/Http/Kernel.phpprotected $middlewareGroups = [
'web' => [
// ...
\BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class,
],
];
Add to .env:
DB_MONITOR_ENABLED=true
DB_MONITOR_SLOW_THRESHOLD=500
DB_MONITOR_N1_THRESHOLD=10
DB_MONITOR_INDEX_THRESHOLD=50
DB_MONITOR_NOTIFY=admin@yourapp.com
DB_MONITOR_RETENTION=7
Config file: config/db-monitor.php
return [
'enabled' => env('DB_MONITOR_ENABLED', true),
'slow_query_threshold_ms' => env('DB_MONITOR_SLOW_THRESHOLD', 500),
'n_plus_one_threshold' => env('DB_MONITOR_N1_THRESHOLD', 10),
'missing_index_min_occurrences' => env('DB_MONITOR_INDEX_THRESHOLD', 50),
'store_queries' => env('DB_MONITOR_STORE_QUERIES', true),
'retention_days' => env('DB_MONITOR_RETENTION', 7),
'notify' => env('DB_MONITOR_NOTIFY', null),
'notification_channels' => ['mail'],
'exclude_paths' => [
'telescope/*',
'_debugbar/*',
'horizon/*',
'livewire/*',
],
];
php artisan db:report
Example output:
DB Monitor Report β Last 24 hours
βΆ SLOW QUERY
Slow query detected: 2300ms
Path: api/orders
π‘ Suggestion: Use select() instead of SELECT *
π‘ Add index:
php artisan db:fix --table=orders --column=user_id
βΆ N+1 QUERY
Same query executed 47 times
π‘ Suggestion: Model::with('user')->get()
βΆ MISSING INDEX
Column orders.user_id used in 230 queries
π‘ Auto-generate migration:
php artisan db:fix --table=orders --column=user_id
php artisan db:report --hours=48
php artisan db:report --severity=critical
php artisan db:report --type=n_plus_one
Generate migration:
php artisan db:fix --table=orders --column=user_id
php artisan migrate
Fix all at once:
php artisan db:fix --all
php artisan migrate
php artisan db:analyze --hours=24
php artisan db:clear --days=7
php artisan db:clear --all
Enable email alerts:
DB_MONITOR_NOTIFY=admin@yourapp.com
Enable Slack notifications in config/db-monitor.php:
'notification_channels' => ['slack'],
Configure Slack in config/services.php:
'slack' => [
'notifications' => [
'bot_user_oauth_token' => env('SLACK_BOT_USER_OAUTH_TOKEN'),
'channel' => env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
],
],
use BenjdiaSaad\DbMonitor\Facades\DbMonitor;
$findings = DbMonitor::runDetectors($queries);
$analysis = DbMonitor::analyzeStoredLogs(hours: 48);
| Command | Description |
|---|---|
db:report |
Show database health report |
db:report --hours=48 |
Report for last 48 hours |
db:report --severity=critical |
Only critical issues |
db:report --type=n_plus_one |
Only N+1 findings |
db:analyze |
Analyze stored logs |
db:fix --table=x --column=y |
Generate index migration |
db:fix --all |
Fix all missing indexes |
db:clear --days=7 |
Clear old logs |
db:clear --all |
Clear everything |
db_monitor_query_logsStores every captured query per request.
db_monitor_findingsStores detected issues.
slow_query, n_plus_one, missing_index)warning, critical)Both tables use Laravel's Prunable trait.
Run manually:
php artisan model:prune
Schedule daily:
Schedule::command('model:prune')->daily();
Unlike traditional profilers, Laravel DB Monitor:
It helps you catch database problems before your users experience slow pages.
MIT β free for personal and commercial use.
Benjdia Saad
GitHub: https://github.com/benjdiasaad
Built to help Laravel developers optimize database performance before it becomes a problem.
How can I help you explore Laravel packages today?