Last active
March 26, 2021 08:16
-
-
Save yavgel85/e17c60cd849df9aff13eb8f3f5541e90 to your computer and use it in GitHub Desktop.
Get the SQL of a Query Builder without the question marks #laravel #queryBuilder
This file contains hidden or 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\Database\Eloquent\Builder; | |
Builder::macro('toSqlWithBindings', function () { | |
$bindings = array_map( | |
fn ($value) => is_numeric($value) ? $value : "'{$value}'", | |
$this->getBindings() | |
); | |
return Str::replaceArray('?', $bindings, $this->toSql()); | |
}); | |
User::where('name', 'Loris')->where('age', 26)->toSql(); | |
// --> select * from `users` where `name` = ? and `age` = ? | |
User::where('name', 'Loris')->where('age', 26)->toSqlWithBindings(); | |
// --> select * from `users` where `name` = 'Loris' and `age` = 26 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment