Laravel Where Like eloquent Query Example

May 28, 2021 . Admin

Hi Dev,

Today, i will explain you how to use and implement wherelike() eloquent query in laravel.

In laravel, utilizing whereLike() eloquent method, you can implement laravel where like search query, laravel where like multiple columns and laravel collection with where like. you can simply and easy to use to define a where like query in laravel.

So let's start to the example laravel whereLike() eloquent methods and follow step.

Example : 1
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $search = "bhavesh";
    
    $users = User::where('name', 'LIKE', "%$search%")
                ->latest()
                ->get();

    dd($users);
}

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

SELECT * FROM `users` WHERE `name` LIKE '%search%';
Example : 2
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $search = "bhavesh";
    
    $users = User::where('name', 'LIKE', "%{$search}%")
                ->latest()
                ->get();

    dd($users);
}
Example : 3
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $search = "bhavesh";
    
    $users = User::where('name', 'LIKE','%' . $search . '%')
                ->latest()
                ->get();

    dd($users);
}

I Hope It Will Help You...

#Laravel 8 #Laravel