Home Backend Development PHP Tutorial Method to implement fuzzy matching and multi-condition query function in Laravel5

Method to implement fuzzy matching and multi-condition query function in Laravel5

Jun 06, 2018 am 11:01 AM
Multi-condition query fuzzy matching

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. Friends in need can refer to it

The example of this article describes the method of implementing fuzzy matching and multi-condition query function in Laravel5. Share it with everyone for your reference, the details are as follows:

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 from the front end and uses encapsulation and splicing to perform fuzzy or precise multi-condition search.

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


The above is the detailed content of Method to implement fuzzy matching and multi-condition query function in Laravel5. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

See all articles