I have a complex query created by a few conditions, and I would like to get the final SQL query from the builder object is about to execute. Can I do that?
For debugging this might come quite handy as it returns the SQL with the bindings, so you can instantly put it into the database console.
/**
* Combines SQL and its bindings
*
* @param \Eloquent $query
* @return string
*/
public static function getEloquentSqlWithBindings($query)
{
return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function ($binding) {
return is_numeric($binding) ? $binding : "'{$binding}'";
})->toArray());
}