Skip to content

Instantly share code, notes, and snippets.

@vitorf7
Forked from arodbits/binder.php
Created August 14, 2019 15:55
Show Gist options
  • Save vitorf7/ba45c70456f12721d213ad27fbc25f39 to your computer and use it in GitHub Desktop.
Save vitorf7/ba45c70456f12721d213ad27fbc25f39 to your computer and use it in GitHub Desktop.
Combine binding values into a raw SQL query in Laravel. Debugging in Laravel.
<?php
//example:
$query = Foo::where('id', '=', 1)->where('name', '=', 'bar');
//would produce the following raw query:
//select * from foo where id = ? and name = ?;
dd(vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function($binding){
return is_numeric($binding) ? $binding : "'{$binding}'";
})->toArray()));
//result:
//select * from foo where id = 1 and name = 'bar';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment