Created
September 25, 2024 11:43
-
-
Save AhmedHelalAhmed/2f33f3c5557669404a833fbfaf44924b to your computer and use it in GitHub Desktop.
Quickly View SQL Queries in Laravel with DB::listen (No Debugbar)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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