[10.x] Add database expressions with bindings #46358
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The goal of this PR is to add a safe method to provide non-atomic database expressions as attribute values in Eloquent.
This is the proposed syntax:
To accomplish it I've added a new kind of expression that I've named
ExpressionWithBindings
. I made the new expression class somewhat compatible with the grammar-dependent ideas of #44784 by @tpetry.While it would be pleasant to add an optional
$bindings
parameter toDB::raw
and agetBindings()
method to the existing interface, it would break compatibility with whoever is extending the class.Instead I've opted to create a completely new interface, a new expression class and an
DB::expression
helper. In addition to leaving more code unaffected, that should also make people feel less incentivized to try this behaviour in other places (likewhere
andorder
clauses) where the new tooling is not supported and other tools (likeorderByRaw($expr, $bindings)
) are already sufficient. Creating a completely new interface and methods also allows to make it all entirely natively typed.