Skip to content

Instantly share code, notes, and snippets.

@vocolboy
Last active November 16, 2018 10:09
Show Gist options
  • Save vocolboy/d6cd7082f2fcbdb7c10893de1df2a088 to your computer and use it in GitHub Desktop.
Save vocolboy/d6cd7082f2fcbdb7c10893de1df2a088 to your computer and use it in GitHub Desktop.
Laravel5.7 Log Query Executed
#Put It In AppServiceProvider.php
<?php
use Illuminate\Database\Events\QueryExecuted;
use DB;
use Log;
...
public function boot()
{
...
DB::listen(function (QueryExecuted $event) {
tap(collect($event->bindings)->map(function ($params) {
return (is_numeric($params)) ? $params : "'$params'";
})->toArray(), function (array $bindings) use ($event) {
Log::info('QueryExecuted', [
'sql' => vsprintf(str_replace('?', '%s', $event->sql), $bindings),
'time' => $event->time,
]);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment