skip to Main Content

Here is my code

$search_term = "Hary Kumar";
$filterData = DB::table('signups')->where('name','LIKE',"%{$search_term}%");

If I am not wrong above code will give me result like:

select * from signups where name like "%Hary Kumar%"

But, I am trying to get

select * from signups where name like "%Hary%" or name like "%Kumar%"

2

Answers


  1. Chosen as BEST ANSWER

    I got my answer. Here is the code:

    $search_values = explode(" ",$search_term);
    
     DB::table('signups')
        ->orWhere(function ($query) use($search_values) {
        foreach($search_values as $search_value)
        $query->orwhere('name', 'like',  '%' . $search_value .'%');})
    

  2. You are looking for a simple orWhere()

    DB::table('signups')
        ->where('name', 'LIKE', "%{$first_term}%")
        ->orWhere('name', 'LIKE', "%{$second_term}%");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search