Created
January 9, 2019 11:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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