[12.x] Add Str::escapeLike() and escape parameter to whereLike #59157
Open
SanderMuller wants to merge 3 commits intolaravel:12.xfrom
Open
[12.x] Add Str::escapeLike() and escape parameter to whereLike #59157SanderMuller wants to merge 3 commits intolaravel:12.xfrom
SanderMuller wants to merge 3 commits intolaravel:12.xfrom
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Str::escapeLike()(andStringable::escapeLike()) to escape%,_, and\characters in LIKE query valuesescapeparameter towhereLike,orWhereLike,whereNotLike, andorWhereNotLikewhereLikewas added to simplify LIKE queries, but it doesn't protect against the most common mistake: user input containing%or_. Addingescape: truemakes the safe path the easy path, whileStr::escapeLike()serves advanced use cases.Usage
The key design goal: adding
escape: trueto an existingwhereLikecall should just work.When
escape: true, leading and trailing%wildcards are preserved and everything in between is escaped. This means all common patterns work naturally:Str::escapeLike()is also available standalone for manual composition:Design decisions
%wildcards: soescape: trueis a drop-in addition to existing queries without restructuring the valueStr, not DB:escapeLikeis a string operation, usable outside of query building%, the boundary preservation can't distinguish it from an intentional wildcard. For this rare case, useStr::escapeLike()directly. In practice, this almost never matters, and if you need an exact match of a value containing%, you'd typically usewhere()instead ofwhereLike()