Home  >  Article  >  Backend Development  >  Method to implement fuzzy matching and multi-condition query function in Laravel5

Method to implement fuzzy matching and multi-condition query function in Laravel5

不言
不言Original
2018-06-06 11:01:502087browse

This article mainly introduces the method of implementing fuzzy matching and multi-condition query function in Laravel5, and analyzes Laravel5 multi-condition fuzzy query and related packaging operation skills in the form of examples. Friends in need can refer to it

The example of this article describes the method of implementing fuzzy matching and multi-condition query function in Laravel5. Share it with everyone for your reference, the details are as follows:

Method 1. ORM mode


##

public function ReportAccurate($data)
{
 if(is_array($data))
 {
   $where = $this->whereAll($data);
   return $where;
 }
 else
 {
   return false;
 }
}
/*多条件模糊*/
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 $data above is the array passed from the front end and uses encapsulation and splicing to perform fuzzy or precise multi-condition search.

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

Method 2. The knowledge used by the master encapsulation method is the 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'];
/*查询的字段*/
$searchFields = [
  'report_identify' => 'like',
  'drug_name' => 'like',
  'event_term' => 'like',
  'organize_role_id' => '=',
  'case_causality' => '=',
  'report_type' => '=',
  'task_user_id' => '=',
  'status' => '=',
];
/*获取查询条件*/
$where = $this->searchArray($searchFields);
/*获取数据*/
$this->reportTaskRepo->pushCriteria(new OrderBySortCriteria('asc', 'task_countdown'));
$data = $this->reportTaskRepo->findWhere($where, $fields);
//在Trait里封装
/**
 * 获取请求中的参数的值
 * @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;
}


The above is the detailed content of Method to implement fuzzy matching and multi-condition query function in Laravel5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn