-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Add whereLike clause to query builder #52147
Add whereLike clause to query builder #52147
Conversation
Docs: laravel/docs#9765 |
@einar-hansen @taylorotwell what about and |
Really like this new method! What about adding an additional parameter to define how to search (start with, end with, any position).
This option should be optional, so you can also use a custom like pattern (for example with Not sure about the search parameter values yet, but I think you'll get the idea. This will make your code more cleaner, because you don't need to format the strings with %'s by yourself. What are your thoughts? |
I've now made a PR for this feature, called Add dynamic "where(Any|All|None)*" clauses to the query builder |
Add whereLike and orWhereLike methods to Query Builder
This PR introduces new methods to the Query Builder for performing LIKE queries with better control over case sensitivity:
whereLike($column, $value, $caseSensitive = false)
: Performs a LIKE query with optional case sensitivitywhereNotLike($column, $value, $caseSensitive = false)
: Performs a NOT LIKE query with optional case sensitivityorWhereLike($column, $value, $caseSensitive = false)
: Adds an OR LIKE clause with optional case sensitivityorWhereNotLike($column, $value, $caseSensitive = false)
: Adds an OR NOT LIKE clause with optional case sensitivityThese methods provide a more intuitive and database-agnostic way to perform string matching queries. They work across all supported databases (MySQL, PostgreSQL, SQLite, and SQL Server) while handling the differences in case-sensitivity behavior. It is by default with case-sensitivity to false, as is the default behavior in MySQL and SQLite.
Benefits:
Example usage:
You want to find a user in the database by looking up its email address, and the user has registered its email address using capital letters in its names
John.Doe@example.com
.If you are using Postgres in production, and SQLite for local development (like me), then you would need to use 'ilike' in Postgres, and 'like' in SQLite, forcing you to write something like this for searching case-insensitive.
After this PR you'll be able to achieve the same by the same query.
or for case-sensitive search
ilike
for case insensitive operations, andlike
for case sensitive.like
for case insensitive operations, andlike binary
for case sensitive.like
for case insensitive operations, and uses glob for case sensitive (converts the binding to use glob, replacing _ and %).like
operation by default. In SQL Server, case sensitivity is determined by the collation of the database or column, therefore this implementation is configured to throw an exception if you set the case-sensitivity flag to true.