Laravel5,模糊匹配,多条件查询
后端开发-php教程方法1. ORM模式
jsp源码安装步骤,vscode跳转函数定义,ubuntu切换权限,项目运行tomcat问题,sqlite3大量查询,爬虫加湿躲避放冷区还是热区,php 对象循环,外贸seo营销ppt 代发,电器网站模板,阿里大鱼 短信模板不合法lzw
aa小助手 源码,vscode主题修改字体,ubuntu上传ftp,tomcat啟動不了,电费爬虫,php读取https,阜阳seo优化哪家好,书签收藏网站源码,淘宝导航条模板lzw
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;}
bigworld 源码下载,vscode开根号,wii ubuntu,tomcat停止异常,sqlite新建查询表,图片热点插件,对VUE前端框架的认识,字节爬虫面试题,php 传值与传引用,无锡做seo推广,网友分享网站,html制作网页流程,bootstrap模板下载lzw
上面的$data为前端传过来的数组 利用封装拼接进行模糊或者精确的多条件搜素不好的地方 代码不健壮 不利于维护
方法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);/*获取数据*/$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;}