php - Laravel search query - SQL ordering of 'OR' and 'AND' ruining foreach loop query -


i having problems search function working properly. currently, have search form 2 radio buttons('keywords' , 'exact') , depending on have selected, should search database given array of strings or single string(both title , description should contain of searched keywords). issue arises when user searches via keywords (a bunch of single word strings). presently when fetch string form, explode , run foreach loop through it, however, ends messing due sql's ordering of 'or' , 'and' operators.

for instance, search value of search_value=black+darker $query->tosql()

"select * `animes` `description` ? or `name` ? , `description` ? or `name` ?" 

however, sql reads as

"select * `animes` (`description` ?) or (`name` ? , `description` ?) or (`name` ?)" 

here code(ignore related 'genre' part works correctly expected):

public function searchanimes(searchrequest $request) {     if($request->search_type == 'keyword')     {         $animesearchvalues = explode(' ', input::get('search_value'));     }     elseif($request->search_type == 'exact')     {         $animesearchvalues[0] = input::get('search_value');     }     else     {         return redirect('animes');     }     $genres = $request->name;     $query = anime::query();      if(isset($animesearchvalues) && strlen(input::get('search_value')) >= 3)     {         foreach($animesearchvalues $animesearchvalue)         {             $query->where('description', 'like', '%'.$animesearchvalue.'%')                   ->orwhere('name', 'like', '%'.$animesearchvalue.'%');         }          if(isset($genres))         {             foreach ($genres $genre)             {                 $query->wherehas('genres', function ($f) use ($genre)                 {                     $f->where('slug', $genre);                 });             }         }         $animeresults = $query->orderby('name')->paginate(10);         return view('animes.results', compact('animeresults'));     } } 

the solution have been able think of take both title , description , merge them single string , search through each individual keyword, however, method seems jerry-rigged or nonstandard. also, keep in mind, need entire thing in single query builder may display results pagination.

thanks.

is wanted? searches has each keyword in either title or description.

foreach($animesearchvalues $animesearchvalue){     $query->where(function ($q) use ($animesearchvalue){         $q->where('description', 'like', '%'.$animesearchvalue.'%')             ->orwhere('name', 'like', '%'.$animesearchvalue.'%');     }); } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -