Home > Article > Backend Development > Detailed explanation of the steps to implement fuzzy matching and multi-condition query function in Laravel5
This time I will bring you a detailed explanation of the steps to implement the fuzzy matching and multi-condition query function in Laravel5. What are the precautions for Laravel5 to implement the fuzzy matching and multi-condition query function. The following is a practical case. One Get up and take a look.
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 splicing to perform fuzzy or precise multiple conditions Sousu
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; }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
thinkPHP5 framework implements paging query steps detailed explanation
PHP detailed steps to obtain the zero o'clock timestamp of the day
The above is the detailed content of Detailed explanation of the steps to implement fuzzy matching and multi-condition query function in Laravel5. For more information, please follow other related articles on the PHP Chinese website!