[10.x] Database Expressions that are conditions #47210
Merged
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.
With #46558 merged, the database expressions could now support more extended use cases: A common thing for database drivers or users is to extend the query builder to support new
whereXyz()
methods. But these are not database-agnostic.I've created a new interface (
ConditionExpression
) to indicate expressions that will be transformed into an entire database condition. These can be passed towhere
andhaving
:Expressions built on the new interface will be handled differently by
WHERE
andHAVING
. At the moment, an expression passed as a single parameter towhere
will result in a SQL string that is no longer the intended use-case. By changing the query builder very minimal to handleConditionExpression
different, those conditions can be embedded into the SQL query correctly:But this approach is much more compelling if you consider more complex use cases. For example, geographical operations or statistical queries require many transformations on columns and values. These are hard to generalize, and you must fall back to raw parts. Some spaghetti code often insecurely interpolates those raw strings when complex customization is used.
With condition expressions, you can safely composite multiple objects to form the condition you want to have. Complex dynamic query-building logic can be transformed into parsers that return a single object that can be used with Laravel: