Method of Fuzzy Matching and Multi condition Query in Laravel5

  • 2021-09-12 00:36:40
  • OfStack

This paper describes the method of realizing fuzzy matching and multi-condition query function in Laravel5 with an example. Share it for your reference, as follows:

Method 1. ORM mode


public function ReportAccurate($data)
{
 if(is_array($data))
 {
   $where = $this->whereAll($data);
   return $where;
 }
 else
 {
   return false;
 }
}
/* Multi-conditional fuzziness */
public function whereAll($data)
{
  $query = new ReportMainpage();
  $results = $query->where(function ($query) use ($data) {
    $data['report_first_received_date'] && $query->where('report_first_received_date', 'like', '%' . $data['report_first_received_date'] . '%');
    $data['report_drug_safety_date'] && $query->where('report_drug_safety_date', 'like', '%' . $data['report_drug_safety_date'] . '%');
    $data['aecountry_id'] && $query->where('aecountry_id', $data['aecountry_id']);
    $data['received_fromid_id'] && $query->where('received_fromid_id', $data['received_fromid_id']);
    $data['research_id'] && $query->where('research_id', 'like', '%' . $data['research_id'] . '%');
    $data['center_number'] && $query->where('center_number', 'like', '%' . $data['center_number'] . '%');
  })->get();
  return $results;
}

The above $data is the array passed from the front end for fuzzy or accurate multi-condition search by encapsulation splicing

The bad part is that the code is not robust and is not conducive to maintenance

Method 2. The knowledge used by the Great God encapsulation method is Repository warehouse


$fields = ['id', 'report_id', 'report_identify', 'report_first_received_date', 'drug_name', 'first_event_term', 'case_serious', 'standard_of_seriousness', 'case_causality', 'received_from_id', 'task_user_name', 'organize_role_name', 'task_countdown', 'report_countdown'];
/* Fields for Query */
$searchFields = [
  'report_identify' => 'like',
  'drug_name' => 'like',
  'event_term' => 'like',
  'organize_role_id' => '=',
  'case_causality' => '=',
  'report_type' => '=',
  'task_user_id' => '=',
  'status' => '=',
];
/* Get query criteria */
$where = $this->searchArray($searchFields);
/* Get data */
$this->reportTaskRepo->pushCriteria(new OrderBySortCriteria('asc', 'task_countdown'));
$data = $this->reportTaskRepo->findWhere($where, $fields);
// In Trait Inner encapsulation 
/**
 *  Gets the value of the parameter in the request 
 * @param array $fields [description]
 * @return [type]     [description]
 */
public function searchArray($fields=[])
{
  $results = [];
  if (is_array($fields)) {
   foreach($fields as $field => $operator) {
     if(request()->has($field) && $value = $this->checkParam($field, '', false)) {
      $results[$field] = [$field, $operator, "%{$value}%"];
     }
   }
  }
  return $results;
}

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to PHP programming based on Laravel framework.


Related articles: