Skip to content

Instantly share code, notes, and snippets.

@lorisleiva
Last active November 15, 2023 08:54
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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