benjdiasaad / laravel-db-monitor
Real-time database health monitoring for Laravel applications. Detects slow queries, N+1 issues, missing indexes, and suggests performance optimizations.
Requires
- php: ^8.2
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/notifications: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
README
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.
β¨ Features
- π’ Detect slow database queries in real time
- π Identify N+1 query problems automatically
- π Suggest missing indexes based on real usage
- π‘ Show actionable fix suggestions
- π§ Generate index migrations automatically
- π¬ Send alerts via Email or Slack
- π Clean CLI reporting (
db:report) - π§Ή Auto-prune old logs using Laravelβs Prunable trait
- β‘ Works with MySQL, PostgreSQL, SQLite
π¦ Installation
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
βοΈ Register the Middleware
Laravel 11 / 12 β bootstrap/app.php
->withMiddleware(function (Middleware $middleware) { $middleware->web(append: [ \BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class, ]); })
Laravel 10 β app/Http/Kernel.php
protected $middlewareGroups = [ 'web' => [ // ... \BenjdiaSaad\DbMonitor\Http\Middleware\MonitorQueries::class, ], ];
π§ Configuration
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/*', ], ];
π Usage
Generate a Database Health Report
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
Filter Reports
php artisan db:report --hours=48 php artisan db:report --severity=critical php artisan db:report --type=n_plus_one
Auto-Fix Missing Indexes
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
Analyze Stored Logs
php artisan db:analyze --hours=24
Clear Logs
php artisan db:clear --days=7 php artisan db:clear --all
π¬ Notifications
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'), ], ],
π§° Facade Usage
use BenjdiaSaad\DbMonitor\Facades\DbMonitor; $findings = DbMonitor::runDetectors($queries); $analysis = DbMonitor::analyzeStoredLogs(hours: 48);
π Artisan Commands
| 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 |
π Database Tables
db_monitor_query_logs
Stores every captured query per request.
- sql
- bindings
- duration_ms
- connection
- request_id
- request_path
db_monitor_findings
Stores detected issues.
- type (
slow_query,n_plus_one,missing_index) - severity (
warning,critical) - message
- context (json)
- request_path
- notified
π§Ή Auto-Pruning
Both tables use Laravel's Prunable trait.
Run manually:
php artisan model:prune
Schedule daily:
Schedule::command('model:prune')->daily();
β Requirements
- PHP 8.2+
- Laravel 10, 11, or 12
- Any Laravel-supported database
π― Why Use Laravel DB Monitor?
Unlike traditional profilers, Laravel DB Monitor:
- Stores historical query patterns
- Detects recurring performance issues
- Suggests fixes automatically
- Can generate index migrations for you
- Works in production environments
It helps you catch database problems before your users experience slow pages.
π License
MIT β free for personal and commercial use.
π€ Author
Benjdia Saad
GitHub: https://github.com/benjdiasaad
Built to help Laravel developers optimize database performance before it becomes a problem.