Laravel 10|9|8 where Like Query Example

Laravel 10|9|8 where Like Query Example

Laravel where like query example. Here you will learn how to use laravel whereLike() eloquent method in different ways with query builder and model.

In laravel, using whereLike() eloquent method, you can implement laravel where like search query, laravel where like multiple columns and laravel collection with where like.

Where Like query in Laravel 10|9|8

Here are some examples of laravel whereLike() eloquent methods:

  • Example 1: where Like Query with Model
  • Example 2: Using macros with Like
  • Example 3: Laravel whereLike with multiple columns using macros

Example 1: where Like Query with Model

You can use the LIKE MySQL keyword and % wildcard character with where clause.

The following example represents, how to use it:

public function index()
    $users = User::where('name','LIKE',"%{$search}%")->get();

When you dump the above given whereNull query you will get the following SQL query:

SELECT * FROM `users` WHERE `name` LIKE '%search%';

Example 2: Using macros with Like

To define a macro, you simply use the macro static method on the class you want to define the macro to. We need to define a macro for the Eloquent class, so we can extend it like this (in the boot method of the service provider):

Builder::macro('whereLike', function($column, $search) {
  return $this->where($column, 'LIKE', "%{$search}%");

Recommended Laravel Post

Create And Uses Of Laravel Macro

The way we can use this macro now is simple:

public function index()
  User::whereLike('username', $username)
   ->whereLike('email', $email)

Example 3: Laravel whereLike with multiple columns using macros

if you want to search with multiple columns then you have to extend this macro to support multiple columns.

See the following:

Builder::macro('whereLike', function($columns, $search) {
  $this->where(function($query) use ($columns, $search) {
    foreach(array_wrap($columns) as $column) {
      $query->orWhere($column, $search);

  return $this;

So now, if we pass a single column (using the array_wrap function we convert it to an array), and search that column, but if we add multiple columns in an array than we loop through all of them and search the search term in all of those columns. Everything is wrapped in an where query because we dont want the whereLike query to mess up any other where queries we can perform on the Eloquent model.

You can use this macro now like this:

public function index()
 User::whereLike(['username', 'email'], $search)
  ->where('enabled', true)


In this laravel whereLike() query example tutorial, you have learned how to use laravel wherelike eloquent method with query builder, model and micros.


Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *