0

I am getting stuck on this. I have two checkboxes i.e. Manicure and pedicure When user select manicure ajax runs and then i want to find the manicure word from all rows Like that:-

enter code here
id   name
 1    Get Manicure
 2    Manicure & Pedicure
 3    Repair Manicure
 4    Get Pedicure
 5    Dummy text

Now suppose i have five records in my table when user clicks on Manicure checkbox i want it must returns the 1,2,3 id and when user click on Both manicure and pedicure i want 1,2,3,4 record and when user clicks on pedicure only 2,4 id must return Can anyone help me how to acheive this functionality.Here is my code

enter code here
$query = Service::whereIn('name',array('Pedicure'))->get();// Only pedicure
$query = Service::whereIn('name',array('Manicure'))->get(); // Only manicure
$query = Service::whereIn('name',array('Manicure','pedicure'))->get(); //Both

I think it might be work with where condition with forloop in between

  • 1
    This is a lot more complex question than you realise. Do you only plan to return a match if the word fully matches a word in the text? For example, if you search for manicure, but the text says manicure**s**, then is this a hit or not? What if the searched word is at the end of a a word in the text? For example, if you search for the word sql and the text says MySQL, then is this a hit? – Shadow May 04 '17 at 10:40
  • this is my client requirement if i user click on services checkbox like pedicure now i want where pedicure is there should be returned if user click on both then both the words exists in name column should be reterived –  May 04 '17 at 10:44
  • 1
    Then you should go back to your client and ask for more detailed specification. – Shadow May 04 '17 at 10:46
  • I think it is possible with for loop i.e. in for loop we put where condition –  May 04 '17 at 10:48
  • It is, but it will not be performant. You should really look into fulltext indexes and fulltext search. However, most of the query has to be constructed via raw sql in this case. – Shadow May 04 '17 at 10:51
  • Possible duplicate of [laravel search multiple words separated by space](http://stackoverflow.com/questions/28270244/laravel-search-multiple-words-separated-by-space) – Shadow May 04 '17 at 10:53
  • The above duplicate give you the loop version, but I'm warning you that it will not be effective. However, if you want to do the right thing, then you will use fulltext indexes and search. Here is an example how to use it (since laravel does not have methods for using it, the link is for a plain php / MySQL example - you can implement this via raw sql query within laravel): http://stackoverflow.com/questions/6717312/mysql-fulltext-search-multiple-words – Shadow May 04 '17 at 10:58

1 Answers1

0

Change your query something like this

//$searchArray = ['Manicure'];
//$searchArray = ['pedicure'];
$searchArray = ['Manicure','pedicure'];
$query = new Service();
$isFirst = true;
foreach($searchArray as $string){
    if($isFirst){
        $query->where('name','like','%'.$string.'%');
        $isFirst = false;
    }else{
        $query->orWhere('name','like','%'.$string.'%');
    }
}
$result = $query->get();
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109