Skip to content

Instantly share code, notes, and snippets.

@azizramdan
Last active September 4, 2023 13:33
Show Gist options
  • Save azizramdan/4d6f8b2ac5180eec70555f74ddcab401 to your computer and use it in GitHub Desktop.
Save azizramdan/4d6f8b2ac5180eec70555f74ddcab401 to your computer and use it in GitHub Desktop.
private function getSql($query)
{
$bindings = $query->getBindings();
return preg_replace_callback('/\?/', function ($match) use (&$bindings, $query) {
return $query->getConnection()->getPdo()->quote(array_shift($bindings));
}, $query->toSql());
}
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
Event::listen(QueryExecuted, function (QueryExecuted $event) {
if ($event->time > 1000) {
Log::info('Slow query detected.', [
// ...
]);
}
});
use Illuminate\Database\Connection;
use Illuminate\Support\Facades\DB;
DB::whenQueryingForLongerThan(1000, function (Connection $connection) {
Log::info('Queries collectively took longer than 1 second.', [
// ...
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment