public function scopeName($query, $searchTerm) { if (empty($searchTerm)) { return $query; } $stripped = DoctorHandler::stripDoctor(urldecode($searchTerm)); if (SearchHelper::hasTwoWords($stripped)) { $nameArray = SearchHelper::getAsTwoWordArray($stripped); // Perform a first_name, last_name search ... return $query->where(function ($query) use($nameArray) { $query->where('first_name', 'like', $nameArray[0] . '%')->Where('last_name', 'like', $nameArray[1] . '%'); }); } else { return $query->where(function ($query) use($stripped) { $query->where('last_name', 'like', $stripped . '%')->orWhere('first_name', 'like', $stripped . '%'); }); } }
/** * Search for physicians by first name, last name or specialty * * @param Request $request * @param int $distance * @return Array */ private function searchWithQuery(Request $request, $distance) { $orderBy = $request->has('order_by') ? $request->order_by : 'distance'; $sort = $request->has('sort') ? $request->sort : 'asc'; $limit = $request->has('per_page') ? $request->per_page : '25'; // Remove forms of "dr." from the beginning of the query $stripped = DoctorHandler::stripDoctor(urldecode($request->q)); if (SearchHelper::hasTwoWords($stripped)) { // Perform a first_name, last_name search ... $nameArray = SearchHelper::getAsTwoWordArray($stripped); $physicians = Physician::withinRadius($request->lat, $request->lon, $distance)->where('first_name', 'like', $nameArray[0] . '%')->where('last_name', 'like', $nameArray[1] . '%')->orderBy($orderBy, $sort)->paginate($limit); } else { // Otherwise try to match on first_name, last_name or specialty $physicians = Physician::withinRadius($request->lat, $request->lon, $distance)->where('last_name', 'like', $stripped . '%')->orWhere('first_name', 'like', $stripped . '%')->orWhere('PrimaryPracticeFocusArea', 'like', $request->q . '%')->orderBy($orderBy, $sort)->paginate($limit); } return $physicians; }
public function testReturnsFalseWhenGivingThreeWords() { $testStrings = ['Hello There how are you', ' x hello there ', ' HELLO ,x THERE ', ' HELLO x, THERE ', 'HELLO,THERE,yo']; foreach ($testStrings as $testString) { $this->assertFalse(SearchHelper::getAsTwoWordArray($testString)); } }