benjdiasaad/laravel-db-monitor

Real-time database health monitoring for Laravel applications. Detects slow queries, N+1 issues, missing indexes, and suggests performance optimizations.

Maintainers

Package info

github.com/benjdiasaad/laravel-db-monitor

pkg:composer/benjdiasaad/laravel-db-monitor

Statistics

Installs: 248

Dependents: 0

Suggesters: 0

Stars: 5

Open Issues: 0

v1.0.0 2026-02-27 11:06 UTC

This package is auto-updated.

Last update: 2026-04-23 17:18:53 UTC


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.

Laravel PHP License

✨ 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.