Home > Software engineering >  search by full text does not return all results
search by full text does not return all results

Time:02-17

I have the following table, with these columns

first_name last_name
maria herrera
maria de herrera

I want to get all the records that have the word "herrera",for which I do the following query

$query = OrgClient::whereRaw("CONCAT(org_clients.first_name,' ',org_clients.last_name)  Ilike ?",["%maria herrera%"])->get();

however this only returns 1 row

first_name last_name
maria herrera

I need it to return both, since both names have the word "herrera", how could this be done?

CodePudding user response:

Add another wildcard

["%maria %herrera%"]

CodePudding user response:

It's not a good idea to search this way, cause functions ignoring indexes.

Separate string by whitespace and add multiple conditions

$words = explode(' ', $searchString);

OrgClient::where(function ($query) use ($words) {
  foreach ($words as $w) {
    // note we dont use first %
    $query->orWhere('org_clients.first_name', 'like', $w . '%');
    $query->orWhere('org_clients.last_name', 'like', $w . '%');
  }
});
  • Related