user7925057
user7925057

Reputation:

how to find the common word from given input in laravel 5.2?

Here i am getting stuck on this. Here is my problem:- I have one table Service

Service table
id     searchservice
1        waxing
2        Waxing
3        American Service
4        Waxing

Now i have one input is:-

 $searchKeyword = Indian waxing

Now i want to find all the records having waxing text i.e. i want to fetch the ids 1,2,4

enter code here
Service::Where('searchservice','like','%'.$searchKeyword.'%')->get();

It gives me empty result. I know in this i have to use some another sql function. Can anyone help me thanks in advance :) . I am using laravel 5.2 framework

Upvotes: 0

Views: 53

Answers (1)

Alex Harris
Alex Harris

Reputation: 6402

You are searching for "Indian waxing" if you want to search for each word individually you would have to do:

$searchKeyword = "Indian waxing"
$keywords = explode(" ", $searchKeyword);

$service = Service::query();
foreach($keywords as $word){
    $service->orWhere('searchservice', 'LIKE', '%'.$word.'%');
}

$services = $service->distinct()->get();

Upvotes: 1

Related Questions