Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A little macro to get the SQL from a query builder without the annoying "?".
use Illuminate\Database\Eloquent\Builder;
Builder::macro('toSqlWithBindings', function () {
$bindings = array_map(
fn ($value) => is_numeric($value) ? $value : "'{$value}'",
return Str::replaceArray('?', $bindings, $this->toSql());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment