Laravel5 implementation of fuzzy matching plus multi-condition query function implementation steps

Source: Internet
Author: User
This time to bring you Laravel5 implementation of fuzzy matching plus multi-conditional query function to realize the steps, Laravel5 realize fuzzy matching plus multi-conditional query function to realize what to note, here is the actual case, take a look.

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;}

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

ThinkPHP5 Framework implementation of paging query steps

PHP get the same day 0 o'clock time stamp step detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.