Skip to content

Instantly share code, notes, and snippets.

@elomatreb
Created February 6, 2016 19:22
Show Gist options
  • Save elomatreb/a28d26a329347af030ae to your computer and use it in GitHub Desktop.
Save elomatreb/a28d26a329347af030ae to your computer and use it in GitHub Desktop.
Laravel Service Provider that logs all SQL queries (with bindings).
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use DB;
use Log;
/**
* Service provider that logs all SQL queries (with bindings).
*
* To use: Place this file in the app/Providers directory and put "App\Providers\LogSqlProvider::class"
* in the "providers" array in your app config (config/app.php).
*/
class LogSqlProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
DB::listen(function ($query) {
Log::debug("[sql] [query] " . $query->sql);
Log::debug("[sql] [bindings] " . print_r($query->bindings, true));
});
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
//
}
}
@realtebo
Copy link

realtebo commented Aug 7, 2018

How can I save to db the query without causing infinite loops?

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