Laravel5中實(shí)現(xiàn)模糊匹配加多條件查詢功能的方法
本文實(shí)例講述了Laravel5中實(shí)現(xiàn)模糊匹配加多條件查詢功能的方法。分享給大家供大家參考,具體如下:
方法1. ORM模式
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;
}
上面的$data為前端傳過來的數(shù)組 利用封裝拼接進(jìn)行模糊或者精確的多條件搜素
不好的地方 代碼不健壯 不利于維護(hù)
方法2. 大神封裝法 利用到的知識是Repository 倉庫
$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);
/*獲取數(shù)據(jù)*/
$this->reportTaskRepo->pushCriteria(new OrderBySortCriteria('asc', 'task_countdown'));
$data = $this->reportTaskRepo->findWhere($where, $fields);
//在Trait里封裝
/**
* 獲取請求中的參數(shù)的值
* @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;
}
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
根據(jù)ip調(diào)用新浪api獲取城市名并轉(zhuǎn)成拼音
這篇文章主要介紹了根據(jù)ip調(diào)用新浪api獲取城市名并轉(zhuǎn)成拼音的示例,,需要的朋友可以參考下2014-03-03
淺談laravel中的關(guān)聯(lián)查詢with的問題
今天小編就為大家分享一篇淺談laravel中的關(guān)聯(lián)查詢with的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記(三) - 單例模式和工廠模式
設(shè)計(jì)模式是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過分類編目的、代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié)。使用設(shè)計(jì)模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性。2014-06-06
PHP list() 將數(shù)組中的值賦給變量的簡單實(shí)例
下面小編就為大家?guī)硪黄狿HP list() 將數(shù)組中的值賦給變量的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
使用session upload_progress實(shí)現(xiàn)文件包含實(shí)例詳解
這篇文章主要為大家介紹了使用session upload_progress實(shí)現(xiàn)文件包含實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
這篇文章主要介紹了thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法,涉及thinkPHP針對表格與Excel文件的操作技巧,需要的朋友可以參考下2015-12-12

