Laravel-5 'LIKE' equivalent Eloquent
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Laravel's Eloquent ORM, the SQL LIKE operator is expressed with a normal where clause rather than a special helper method. Once you know that pattern, wildcard searches, prefix matches, suffix matches, and relation-based text filters all become straightforward.
The Basic Eloquent Pattern
The direct equivalent of SQL LIKE is:
That translates naturally to SQL similar to:
The three arguments are:
- the column name
- the operator, which is
'like' - the search pattern, including any
%or_wildcards
Laravel passes the condition through the query builder, so you can combine it with the rest of your normal Eloquent chain.
Common Search Patterns
Use % when you want any number of characters around your search term.
Use _ for a single-character wildcard:
That matches values such as ABC1 or ABZ1, but not ABCD1.
Combining LIKE Clauses
Because LIKE is just part of the query builder, you can combine it with other filters.
You can also use orWhere:
For more complex logic, wrap the related conditions in a closure.
This keeps the generated SQL grouped correctly.
Using LIKE on Relationships
Searching a related model is another common use case. For example, suppose a Post belongs to a User, and you want posts whose author name matches a pattern.
That is often cleaner than manually writing joins when you already have Eloquent relationships defined.
Case Sensitivity Depends on the Database
One subtle point is that LIKE behavior is partly database-specific. In MySQL, case sensitivity often depends on the column collation. In PostgreSQL, LIKE is case-sensitive and ILIKE is the case-insensitive variant.
In Laravel, a PostgreSQL case-insensitive search usually looks like this:
So the Eloquent pattern is the same, but the operator string may change based on the database backend.
Escaping Wildcards When Input Comes from Users
If a user types % or _, those characters act as wildcards in SQL. Sometimes that is desirable, but often you want to search for them literally.
At minimum, sanitize or escape user input before building the pattern:
The exact escape behavior can vary by database, so test this on the engine you actually run in production.
When to Use whereRaw
Most of the time, plain where(..., 'like', ...) is enough. Reach for whereRaw only when you truly need database-specific logic, such as explicit collations or functions around the column.
That works, but it gives up some of the clarity and safety of the standard builder call. Keep it as the exception, not the default.
Common Pitfalls
The first pitfall is forgetting the wildcard characters. where('name', 'like', 'john') behaves more like an exact match than a contains search.
Another pitfall is assuming case sensitivity is the same on every database. The same Eloquent code can behave differently on MySQL and PostgreSQL if you do not account for collation or ILIKE.
A third pitfall is interpolating user input carelessly into a raw SQL fragment. Prefer normal query-builder binding whenever possible.
Finally, be realistic about performance. A pattern such as '%john%' can be expensive on large tables because it often prevents efficient index use. Prefix searches such as 'john%' are usually friendlier to indexing.
Summary
- In Eloquent, SQL
LIKEis written withwhere('column', 'like', 'pattern') - Use
%for multi-character wildcards and_for a single-character wildcard - Combine
LIKEwith normalwhere,orWhere, andwhereHaschains - Case sensitivity depends on the database engine and collation rules
- Escape user-supplied wildcard characters and be mindful of performance on large tables

