Skip to content

Instantly share code, notes, and snippets.

@lorisleiva
Last active November 12, 2024 12:14
Show Gist options
  • Save lorisleiva/bdbb5aece08b674a7cb9511934b3e049 to your computer and use it in GitHub Desktop.
Save lorisleiva/bdbb5aece08b674a7cb9511934b3e049 to your computer and use it in GitHub Desktop.
A little macro to get the SQL from a query builder without the annoying "?".
<?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());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment