Home Backend Development PHP Tutorial Detailed explanation of the steps to implement fuzzy matching and multi-condition query function in Laravel5

Detailed explanation of the steps to implement fuzzy matching and multi-condition query function in Laravel5

May 17, 2018 pm 02:29 PM
match

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

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

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!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Practical Guide to Regular Expressions in Go: How to Match Hexadecimal Color Codes Jul 13, 2023 am 10:46 AM

Go Language Regular Expressions Practical Guide: How to Match Hexadecimal Color Codes Introduction: Regular expressions are a powerful and flexible tool for pattern matching and finding strings. In Go language, we can use the built-in regular expression package regexp to implement these operations. This article will introduce how to use regular expressions to match hexadecimal color codes in Go language. Importing the regular expression package First, we need to import the regular expression package regexp of the Go language. You can add the following import statement at the beginning of the code: i

PHP regular expression in action: matching letters and numbers PHP regular expression in action: matching letters and numbers Jun 22, 2023 pm 04:49 PM

PHP regular expression practice: matching letters and numbers Regular expression is a tool used to match strings, which can easily realize string search, replacement, split and other operations. Regular expressions are also a very useful tool in PHP development. This article will introduce how to use PHP regular expressions to match letters and numbers. Matching a Single Character To match a single character, you can use the character classes in regular expressions. Character classes are represented by square brackets []. The characters in them represent the characters that can be matched. You can use hyphens - to represent ranges.

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions In PHP development, string matching is a common task, usually used to find specific text content or to verify the format of input. However, sometimes we need to avoid using ambiguous inclusion expressions to ensure match accuracy. This article will introduce some techniques to avoid ambiguous inclusion expressions when doing string matching in PHP, and provide specific code examples. Use preg_match() function for exact matching In PHP, you can use preg_mat

How to match in Jedi Submarine 2 How to match in Jedi Submarine 2 Feb 27, 2024 pm 08:43 PM

Jedi Submarine 2 is a third-person shooting game with high-quality masterpiece gameplay. It has a lot of exciting gameplay that allows friends to explore the operational fun of online shooting battles. The online mode in the game can be matched. Some players I still don’t know how to operate matching. In this issue, I will share the matching steps with you! Matching operation tutorial of Jedi Submarine 2. Answer: Click Quick Match on the planet interface. The matching method of Jedi Submarine 2. The quick matching of Jedi Submarine 2 is a very good function. It can help players find teammates to match together, enter a mission together, and cooperate with each other to obtain a higher mission evaluation. The matching options are on the planet interface. When looking for tasks or viewing public rooms, there will be a quick match below. Click to start matching. If the player turns on cross leveling

PHP Regular Expression: How to match all textarea tags in HTML PHP Regular Expression: How to match all textarea tags in HTML Jun 22, 2023 pm 09:27 PM

HTML is a commonly used page markup language used to display content on web pages. In HTML, the textarea tag is used to create text boxes that allow users to enter or edit text. When you need to extract all textarea tags and their contents from a page, PHP regular expressions can provide a simple and effective solution. In this article, we will learn how to match all textarea tags in HTML using PHP regular expressions. Understand regular tables

Type mismatch in Java - java.lang.ClassCastException Type mismatch in Java - java.lang.ClassCastException Jun 24, 2023 pm 09:30 PM

As a strongly typed language, Java requires that the types of variables must be clearly determined at compile time, which ensures the security of the program to a certain extent. But sometimes, at runtime, we may encounter a type conversion exception - java.lang.ClassCastException. This exception will appear in a Java program. When the program tries to convert an object to an incompatible type, This exception will be thrown. Java.lang.ClassCastExcepti

How to use regular expressions in PHP to match multiple consecutive specific characters How to use regular expressions in PHP to match multiple consecutive specific characters Jun 22, 2023 pm 08:15 PM

Regular expressions are a powerful text processing tool that are widely used in PHP. One common usage is to match multiple consecutive specific characters, such as matching multiple consecutive spaces, multiple consecutive commas, etc. This article will introduce how to use regular expressions in PHP to achieve this function. In PHP, we can use the preg_match() function to perform regular expression matching. This function requires two parameters: the regular expression and the string to be matched. If the match is successful

See all articles