This article mainly introduces the method of realizing fuzzy matching plus multi-conditional query in Laravel5, analyzes the LARAVEL5 multi-condition fuzzy query and related encapsulation operation skill with the example form, and needs friends to refer to
This paper describes the method of implementing fuzzy matching and multi-conditional query in Laravel5. Share to everyone 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 fuzzy */public function Whereall ($data) {$query = new reportmainpage (); $results = $query->where (function ($query) use ($data) {$data [' report_first_received_date '] && $query->w Here (' 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;}
Above the $data for the front end of the array to use the package stitching for fuzzy or accurate multi-condition search.
Bad place code is not robust and is not conducive to maintenance
Method 2. The knowledge that the great God encapsulation method uses 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 '];/* query field */$searchFields = [' report_identify ' = ' like ', ' drug_name ' = ' ', ' event_term ' = ' like ', ' organize_role_id ' = ' = ', ' case_causality ' = ' = ', ' report_type ' = ' = ', ' Tas k_user_id ' = ' = ', ' status ' = ' = ',];/* gets the query condition */$where = $this->searcharray ($searchFields);/* Get data */$this Reporttaskrepo->pushcriteria (New Orderbysortcriteria (' ASC ', ' Task_countdown ')); $data = $this Reporttaskrepo->findwhere ($where, $fields);//package/** in trait * 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;}