DEV Community

Benjdia Saad
Benjdia Saad

Posted on

Stop Guessing Why Your Laravel App Is Slow — This Free Package Diagnoses & Fixes It For You

Most Laravel performance guides stop at "use eager loading" and "add indexes." Cool advice. But how do you know which relationships are lazy-loaded in production? Which indexes are actually missing? And when you find them, how long does it take to write the migration?

I got tired of that manual loop — profile, guess, fix, repeat. So I built laravel-db-monitor, a package that does the full cycle: detect → analyze → generate the fix.


What it actually does

The package hooks into Laravel's query listener via middleware and silently records three categories of problems:

1. Slow queries — anything over your threshold (default 500ms, fully configurable). No APM subscription needed.

2. N+1 patterns — it tracks when the same query pattern fires 10+ times in a single request. That's the silent killer most developers only catch after a traffic spike.

3. Missing indexes — it analyzes your actual query patterns and figures out which columns you're filtering on without an index.

What makes it different from just enabling Laravel Telescope? It goes further: it gives you the fix, not just the symptom.


Install in 3 steps

composer require benjdiasaad/laravel-db-monitor

php artisan vendor:publish --tag=db-monitor-config
php artisan vendor:publish --tag=db-monitor-migrations
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Then register the middleware in bootstrap/app.php (Laravel 11/12) or Kernel.php (Laravel 10). That's it — monitoring starts immediately with zero config.


The part I'm most proud of: db:fix

After running for a bit, pull a full health report:

php artisan db:report
Enter fullscreen mode Exit fullscreen mode

And when it surfaces a missing index, you don't need to write a migration by hand. Just:

php artisan db:fix --table=orders --column=user_id
Enter fullscreen mode Exit fullscreen mode

It generates a ready-to-run migration file. One command from diagnosis to fix.


Configure it to fit your environment

Everything is driven by env variables, so you can tune it per environment:

DB_MONITOR_ENABLED=true
DB_MONITOR_SLOW_THRESHOLD=300
DB_MONITOR_N1_THRESHOLD=10
DB_MONITOR_NOTIFY=team@yourapp.com
DB_MONITOR_RETENTION=30
Enter fullscreen mode Exit fullscreen mode

You can also get Slack alerts for critical issues — useful for catching regressions after deploys.


Artisan commands at a glance

Command What it does
db:report Full health report with actionable fixes
db:fix --table=x --column=y Generates an index migration
db:analyze Processes stored logs into findings
db:clear Prunes old log data

Works with your stack

  • ✅ Laravel 10, 11, 12
  • ✅ PHP 8.2+
  • ✅ MySQL, PostgreSQL, SQLite
  • ✅ MIT License — free forever

Why I built this

I was working on a project where a single endpoint was generating 80+ queries per request. Telescope showed me the symptoms. But connecting the dots — which model, which relationship, which column needed the index — was still manual work.

I wanted a tool that closes that loop automatically. This is it.


GitHub → github.com/benjdiasaad/laravel-db-monitor

If this saves you debugging time, drop a ⭐ on the repo. PRs and feedback welcome!

Top comments (0)