Skip to content

Instantly share code, notes, and snippets.

@matthew-inamdar
Created January 9, 2019 11:18
Show Gist options
  • Save matthew-inamdar/4a8e322aeba03c50444f5e305a7a0e9d to your computer and use it in GitHub Desktop.
Save matthew-inamdar/4a8e322aeba03c50444f5e305a7a0e9d to your computer and use it in GitHub Desktop.
Function to combine a Laravel query with the bindings and output as a string.
<?php
/**
* Outputs the query with bindings inserted.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return string
*/
function combine_query(\Illuminate\Database\Eloquent\Builder $query): string
{
return vsprintf(
str_replace('?', '%s', $query->toSql()),
collect($query->getBindings())->map(function ($binding) {
return is_numeric($binding) ? $binding : "'{$binding}'";
})->toArray()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment