halilcosdu/laravel-slower
Laravel Slower spots slow database queries in Laravel apps and logs them for review. Optional AI recommendations suggest indexes and performance improvements so you can optimize query speed during debugging or ongoing monitoring.
composer require halilcosdu/laravel-slower
php artisan vendor:publish --tag="slower-config"
php artisan vendor:publish --tag="slower-migrations"
php artisan migrate
OPENAI_API_KEY and optionally OPENAI_ORGANIZATION in .env. Set SLOWER_ENABLED=true and SLOWER_THRESHOLD (in milliseconds) to define what counts as a “slow” query.// In app/Console/Kernel.php
$schedule->command('slower:analyze')->daily();
$schedule->command('slower:clean', ['--days' => 30])->daily();
slower:analyze daily (or hourly) via Laravel Scheduler. It processes pending logs (is_analyzed = false) and uses OpenAI to generate optimization recommendations. Because the command runs ->runInBackground(), it doesn’t block the console worker.$log = SlowLog::where('time', '>', 5)->first();
Slower::analyze($log);
echo $log->recommendation;
SLOWER_THRESHOLD) to set aggressive monitoring (e.g., SLOWER_THRESHOLD=5000) in staging and relax it in production (15000) to avoid noise.DB::listen()) or tools like Laravel Debugbar — this package doesn’t interfere; it consumes its own log table.SLOWER_ENABLED=false locally to avoid logging minor fluctuations; keep it enabled only in non-local environments.ai_recommendation defaults to true. If OPENAI_API_KEY is missing, it throws an exception — handle this with a try/catch or disable AI via config if budget is a concern.EXPLAIN ANALYZE output (or MySQL’s EXPLAIN FORMAT=JSON if DB supports it) for better suggestions. Ensure your DB user has sufficient privileges to run EXPLAIN ANALYZE.INSERT, UPDATE, DELETE, and queries with EXPLAIN prefix are skipped. Adjust via config if your workload includes long-running DML.information_schema or equivalent.SLOWER_PROMPT or use config('slower.prompt') to inject environment-specific context (e.g., highlight read replicas or sharding strategy).SlowLog model uses longtext for sql, raw_sql, recommendation, etc. Ensure your DB supports that (all major ones do). If using SQLite for local dev, prefer text or mediumtext to avoid bloat.How can I help you explore Laravel packages today?