What You’ll Learn
Setting up DB::listen() with a configurable threshold
Logging slow queries to a dedicated query channel
Reconstructing the full SQL with bound parameters
The DB::prohibitDestructiveCommands() safety rail
Model::shouldBeStrict() for N+1 detection in development
Why Slow Queries Matter
Slow queries are the most common cause of slow pages in Laravel applications. They’re also the hardest to find post-production because:
They only appear under real load
Individual pages may seem fine until traffic increases
They compound: a 3-second query running on every pageload is invisible in development but catastrophic at 100 concurrent users
Proactive slow query logging catches them before users experience them.
DB::listen() in AppServiceProvider
Register the listener in AppServiceProvider::boot() :
// app/Providers/AppServiceProvider.php public function boot ( ) : void { $this -> registerSlowQueryDetection ( ) ; // ...…