简体   繁体   中英

Search Query not functioning properly in Laravel

public static function searchSubmissions($request, $candidateIds = array(),$isscontact = NULL){
      $querys = DB::table('submissions')->select('submissions.*');
  if ( Input::has('candidatename') and $request->input('candidatename') != NULL){
      $querys->join('candidates','candidates.candidateid','=','submissions.candidateid');
      $querys->where('candidates.firstname', 'LIKE', '%'. $request->input('candidatename').'%')->orWhere('candidates.lastname', 'LIKE', '%'. $request->input('candidatename') .'%')->where('submissions.status','1');
  }
   $result = $querys->paginate(PAGELIMIT);

  return $result ;
}

This is my search query if I entered "john" or "peter" in my search field it works fine. if I entered full name "john peter" it does not work for me. In my database I have two fields 'firstname' 'lastname'

I think, you need to refactor your code to work it properly.

$names = explode(' ', $request->input('candidatename'));

if (!empty($names)) {
    foreach ($names as $name) {
        $querys = $querys->orWhere('candidates.firstname', 'LIKE', '%'. $name .'%');
        $querys = $querys->orWhere('candidates.lastname', 'LIKE', '%'. $name .'%');
    }
}

Have a look at this https://laravel.com/docs/5.4/queries#parameter-grouping

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM