Works on 5.6, something like this in AppServiceProvider :: boot ()
// Log all DB SELECT statements // @codeCoverageIgnoreStart if (!app()->environment('testing') && config('app.log_sql')) { DB::listen(function ($query) { if (preg_match('/^select/', $query->sql)) { Log::info('sql: ' . $query->sql); // Also available are $query->bindings and $query->time. } }); }
Then in config / app.php so that it is easy to enable / disable the .env change
'log_sql' => env('LOG_SQL'),
All credits: https://arjunphp.com/laravel-5-5-log-eloquent-queries/
And this can be analyzed for unique queries with:
grep ") sql:" laravel.log | sed -e "s#.*select\(.*\)\[\]#select\1#" | sort -u
source share