Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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';
@havietduc91

This comment has been minimized.

Copy link

commented Jun 8, 2018

Thank @thonyx, it's saved my time a lot!

@andreshg112

This comment has been minimized.

Copy link

commented Dec 18, 2018

Thanks! It also worked for me!

@brayandemarchi

This comment has been minimized.

Copy link

commented Mar 15, 2019

works for me too, thanks bro!!

@spaceemotion

This comment has been minimized.

Copy link

commented Mar 29, 2019

Thanks a lot! Here's a version without using the Laravel Collection:

return vsprintf(str_replace('?', '%s', $query->toSql()), array_map(function($binding) {
    return is_numeric($binding) ? $binding : "'{$binding}'";
}, $query->getBindings()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.