Last active
September 21, 2023 09:41
-
-
Save ziadoz/12272309683b0e365d8c119c342dc149 to your computer and use it in GitHub Desktop.
Laravel - QueryExecuted To Raw SQL
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 | |
// Before Laravel 10.15.0 | |
function prepareQuery(QueryExecuted $query): string | |
{ | |
return count($query->bindings) > 0 | |
? vsprintf(str_replace(['%', '?'], ['%%', '%s'], $query->sql), array_map(fn ($value) => (is_numeric($value) ? $value : "'" . $value . "'"), $query->bindings)) | |
: $query->sql; | |
} | |
prepareQuery($query); | |
// After Laravel 10.15.0 | |
function prepareSql(QueryExecuted $query): string | |
{ | |
return $query->connection->getQueryGrammar()->substituteBindingsIntoRawSql( | |
$query->sql, | |
$query->connection->prepareBindings($query->bindings), | |
); | |
} | |
// Or from a built query: | |
$query->toRawSql(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment