Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Query builder to sql with bindings #35910

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ class Builder
* @var string[]
*/
protected $passthru = [
'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd',
'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection', 'raw', 'getGrammar',
'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'toSqlWithBindings',
'dump', 'dd', 'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection', 'raw',
'getGrammar',
];

/**
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2264,6 +2264,30 @@ public function toSql()
return $this->grammar->compileSelect($this);
}

/**
* Get the SQL representation of the query with bindings.
*
* @return string
*/
public function toSqlWithBindings()
{
$bindings = array_map(
function ($value) {
if (is_numeric($value)) {
return $value;
}

if (is_bool($value)) {
return $value ? 1 : 0;
}

return "'$value'";
}, $this->getBindings()
);

return Str::replaceArray('?', $bindings, $this->toSql());
}

/**
* Execute a query for a single record by ID.
*
Expand Down