Skip to content

Instantly share code, notes, and snippets.

@jandaryl
Created September 28, 2018 00:35
Show Gist options
  • Select an option

  • Save jandaryl/fa783034c3397392a21dec951ccb2ac9 to your computer and use it in GitHub Desktop.

Select an option

Save jandaryl/fa783034c3397392a21dec951ccb2ac9 to your computer and use it in GitHub Desktop.
Easy way to log all SQL queries in Laravel Application

Easy way to log all SQL queries in Laravel Application

Every developer, when working on a project, start at some point wandering about the performances of his application. In the Laravel world, the most frequent question you may have is "are my sql queries fast enough ?". If Eloquent is doing a great job optimizing your queries, sometines it's not enough.

If you want to analyze the SQL queries of your application, behind Eloquent, you then need some tool to log all the executed queries. Laravel Debugbar is a great tool for that and it will help you lat.

For some projects however, you don't especially want to setup the Laravel Debugbar package. In this case, how can you log SQL queries ?

There is a simple snippet that you can add in the AppServiceProvider that will help you quite a lot.

In the boot() method:

\DB::listen(function($query) { \Log::info($query->sql, $query->bindings); });

Now, when running you application, every single query will be written down in the laravel.log file, located in storage/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment