A method of implementing fuzzy matching and multi-conditional query in Laravel5

Source: Internet
Author: User
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;}


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.