Laravel Eloquent WHERE Like Query Example

Laravel eloquent where Like query example; Through this tutorial, i am going to show you how to use where Like eloquent query in laravel apps.

Laravel Eloquent WHERE Like Query Example

Let’s see the following examples of where like query with micros in laravel apps; is as follows:

  • Example 1: Laravel where Like Query with Eloquent Model
  • Example 2: Laravel where Like Query with macros

Example 1: Laravel where Like Query with Eloquent Model

Let’s see the following example of where like query with eloquent model in laravel apps; is as follows:

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

If you dump the above given query, will get the following SQL query:

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

Example 2: Laravel where Like Query with macros

Let’s see the following example of where like query with macros in laravel apps; is as follows:

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

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.