Laravel
Eloquent
SQL
Query Builder
Database Queries

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:

php
User::where('name', 'like', '%john%')->get();

That translates naturally to SQL similar to:

sql
SELECT * FROM users WHERE name LIKE '%john%';

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.

php
1// Contains "john"
2$users = User::where('name', 'like', '%john%')->get();
3
4// Starts with "john"
5$users = User::where('name', 'like', 'john%')->get();
6
7// Ends with "john"
8$users = User::where('name', 'like', '%john')->get();

Use _ for a single-character wildcard:

php
$codes = Product::where('code', 'like', 'AB_1')->get();

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.

php
1$users = User::where('name', 'like', '%john%')
2    ->where('is_active', true)
3    ->orderBy('name')
4    ->get();

You can also use orWhere:

php
$users = User::where('name', 'like', '%john%')
    ->orWhere('email', 'like', '%john%')
    ->get();

For more complex logic, wrap the related conditions in a closure.

php
1$users = User::where(function ($query) {
2        $query->where('name', 'like', '%john%')
3              ->orWhere('email', 'like', '%john%');
4    })
5    ->where('is_active', true)
6    ->get();

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.

php
$posts = Post::whereHas('user', function ($query) {
    $query->where('name', 'like', '%john%');
})->get();

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:

php
$users = User::where('name', 'ilike', '%john%')->get();

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:

php
$term = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $request->input('term'));
$users = User::where('name', 'like', '%' . $term . '%')->get();

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.

php
$users = User::whereRaw('LOWER(name) LIKE ?', ['%john%'])->get();

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 LIKE is written with where('column', 'like', 'pattern')
  • Use % for multi-character wildcards and _ for a single-character wildcard
  • Combine LIKE with normal where, orWhere, and whereHas chains
  • Case sensitivity depends on the database engine and collation rules
  • Escape user-supplied wildcard characters and be mindful of performance on large tables

Course illustration
Course illustration

All Rights Reserved.