Siva Ganesh
Siva Ganesh

Reputation: 1445

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'

Upvotes: 0

Views: 76

Answers (1)

Rafee
Rafee

Reputation: 4078

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

Upvotes: 3

Related Questions