Skip to content

Instantly share code, notes, and snippets.

@AhmedHelalAhmed
Created September 25, 2024 11:43
Show Gist options
  • Save AhmedHelalAhmed/2f33f3c5557669404a833fbfaf44924b to your computer and use it in GitHub Desktop.
Save AhmedHelalAhmed/2f33f3c5557669404a833fbfaf44924b to your computer and use it in GitHub Desktop.
Quickly View SQL Queries in Laravel with DB::listen (No Debugbar)
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Events\QueryExecuted;
DB::listen(function (QueryExecuted $query) use (&$count) {
$count++;
$bindings = collect($query->bindings)
->map(function ($param) {
if (is_numeric($param)) {
return $param;
} else {
return "'${param}'";
}
});
dump("========================( START QUERY ($count) )===============================");
echo "\n\n";
dump(Str::replaceArray('?', $bindings->toArray(), $query->sql));
echo "\n\n";
dump("========================( END QUERY ($count) )===============================");
echo "\n\n";
});
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Events\QueryExecuted;
DB::listen(function (QueryExecuted $query) use (&$count) {
$count++;
dump("========================( START QUERY ($count) )===============================");
echo "\n\n";
dump($query->toRawSql());
echo "\n\n";
dump("========================( END QUERY ($count) )===============================");
echo "\n\n";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment