Laravel5 implements fuzzy matching function

小云云
Release: 2023-03-22 09:32:01
Original
2254 people have browsed it

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. I hope it can help everyone.

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;
}
Copy after login

The $data above is the array passed by the front end. Encapsulation and splicing for fuzzy or precise multi-condition search

The disadvantages are that the code is not robust and is not conducive to maintenance

Method 2. The knowledge utilized by the master’s 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'];
/*查询的字段*/
$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;
}
Copy after login

Related recommendations:

Example of fuzzy matching function of characters using regular expressions

JS imitation Baidu automatic drop-down box fuzzy matching prompt

javascript regular expression fuzzy matching IP address function example

The above is the detailed content of Laravel5 implements fuzzy matching function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!