Laravel 10 Eloquent WHERE Like Query Example Tutorial

Laravel 10 Eloquent WHERE Like Query Example Tutorial

In this post we will give you information about Laravel 10 Eloquent WHERE Like Query Example Tutorial. Hear we will give you detail about Laravel 10 Eloquent WHERE Like Query Example Tutorial And how to use it also give you demo for it if it is necessary.

When you put the search form in your application, you need to use like query to get matched pattern. The LIKE a query is used in a WHERE clause to search for a specified pattern in a column. You can use the LIKE MySQL keyword and % wildcard character with where clause.

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.

Example 1: Laravel where Like Query use with Eloquent Model

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

The following example represents, how to use it:

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

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

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

Also see: Laravel Multiple Where Condition Example

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}%");
});

The way we can use this macro now is simple:

public function index(){
  return User::whereLike('username', $username)
   ->whereLike('email', $email)
   ->get();
}

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.

Builder::macro('whereLike', function($columns, $search) {
  $this->where(function($query) use ($columns, $search) {
    foreach(Arr::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 then 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 don’t 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(){
 return User::whereLike(['username', 'email'], $search)->get();
}

Thank you for reading this article.

Also see: How to send a DELETE request with cURL

  .       .

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Hope this code and post will helped you for implement Laravel 10 Eloquent WHERE Like Query Example Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US