laravel 中条件查询 function模式
当需要条件查找时,可以使用下面的注入方法:
//我要预约 yudoc_name yudoc_keshi yudoc_jibing yudoc_hospital 这是需要帅选的条件
public function userYuYueDoc(Request $r) {
$data = $r->all();
//获取医生
$res = \DB::table('dxjk_yudoc')
->where('yudoc_status',2)
->where(function($query)use($data){
if($data['yudoc_name'] !== ''){//这一步成立才会查询
$query->where('yudoc_name','like', '%'.$data['yudoc_name'].'%');
}
})
->where(function($query)use($data){
if($data['yudoc_jibing'] !== ''){
//按疾病查找
$jibing_res = \DB::table('dxjk_yudoc')
->select('yudoc_id','yudoc_jibing')
->get();
$doc_id = array();
if(!empty($jibing_res)){
foreach($jibing_res as $key => $val){
$val->yudoc_jibing = json_decode($val->yudoc_jibing,true);
if(in_array($data['yudoc_jibing'],$val->yudoc_jibing)){
array_push($doc_id,$val->yudoc_id);
}
}
}
$doc_id = array_unique($doc_id);
$query->whereIn('yudoc_id', $doc_id);
}
})
->where(function($query)use($data){
if($data['yudoc_keshi'] !== ''){
$query->where('yudoc_keshi', $data['yudoc_keshi']);
}
})
->where(function($query)use($data){
if($data['yudoc_hospital'] !== ''){
//查找该医院所属医生,并去重,组成数组
$doc_res = \DB::table('dxjk_docwork')
->select('docwork_yid')
->where('docwork_hospital',$data['yudoc_hospital'])
->get();
$doc_id = array();
if(!empty($doc_res)){
foreach($doc_res as $key => $val){
array_push($doc_id,$val->docwork_yid);
}
}
$doc_id = array_unique($doc_id);
$query->whereIn('yudoc_id', $doc_id);
}
})
->orderBy('yudoc_tuijian')
->paginate(10)
->toArray();
if (!empty($res['data'])) {
//将查询的时间和图片处理下
$basePath = base_path();
$basePath = $basePath."/public/uploads";
foreach($res['data'] as $key => $val){
//获取医生的出诊医院
$hospital = \DB::table('dxjk_docwork')
->select('docwork_hospital')
->where('docwork_yid',$val->yudoc_id)
->distinct()
->get();
if(!empty($hospital)){
$val->yudoc_hospital = $hospital;
$yudoc_money = \DB::table('dxjk_docwork')
->select('docwork_money')
->where('docwork_yid',$val->yudoc_id)
->where('docwork_hospital',$hospital[0]->docwork_hospital)
->distinct()
->get();
$val->yudoc_money = $yudoc_money[0]->docwork_money;
} $val->yudoc_time = date('Y-m-d',$val->yudoc_time);
$val->yudoc_pic = json_decode($val->yudoc_pic,true);
if(!empty($val->yudoc_pic)){
foreach($val->yudoc_pic as $k => $v){
// $img_file = $basePath.$v;
// $img_info = getimagesize($img_file);
// $val->yudoc_pic[$k] = "data:{$img_info['mime']};base64," . base64_encode(file_get_contents($img_file));
$val->yudoc_pic[$k] = self::urlPic . $v;
}
}
$val->yudoc_jibing = json_decode($val->yudoc_jibing,true);
}
return ['code' => 1000, 'data' => $res];
}else{
return ['code' => 1001, 'data' => ['message' => '未查到医生!']];
} }
laravel 中条件查询 function模式的更多相关文章
- Laravel中服务提供者和门面模式
在laravel中,我们可能需要用到自己添加的类时,可以建立一个文件夹专门存放类文件,也可以使用laravel的服务提供者的方式来使用. 这两者其实区别不大,主要是前者使用的话,会跟业务代码产生依赖, ...
- laravel中通过查询构造器,实现数据的curd
//查询构造器: public function query1(){ //利用查询构造器,插入数据: /*$num=DB::table('student')->insert( ['name'=& ...
- laravel 中数据库查询结果自动转数组
今天在项目中再次碰见laravel中从数据库中查询到的结果是对象,用toArray也不好用,之前改过一次,印象中是在/confing/database.php, 'fetch' => PD ...
- Mysql数据库中条件查询
1.concat(字符串拼接) 作用:将选中的列进行拼接 写法 AS的作用就是属性名 SELECT CONCAT(ename,job) AS 你猜 FROM emp; 2.条件查询 语法: sele ...
- laravel多条件查询,及分页
$res = DtkModel::where('ID','>','1')->select("ID")->get()->paginate(20);//不成立 ...
- Laravel中的查询构造器
public function query(){ //新增数据 //$bool = DB::table('wd_user')->insert(['username'=>'jack']); ...
- laravel 多条件查询
select * from homework where (id between 1 and 10 or id between 50 and 70) and complete = 1 and (tit ...
- laravel多条件查询(and,or嵌套查询)
原生sql select * from homework where (id between 1 and 10 or id between 50 and 70) and complete = 1 an ...
- laravel带条件查询手动分页
后台php代码: //手动分页 $users = $kaoqin; //打算输出的数组,二维 $perPage = 10; if ($request->has('page')) { $curre ...
随机推荐
- centos7 部署 dotnetcore
一.在centos上下载dotnetcore SDK 二. 选择Linux发行版安装 按上面的步骤安装,在centos终端输入dotnet --version 显示版本信息即安装成功
- Spring中Value注解的使用
Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...
- 安装pysqlite2
1. 从https://github.com/msabramo/pysqlite2 下载源码. 2.安装python-dev: sudo apt-get install python-dev 否则在 ...
- 一个Elasticsearch嵌套nested查询的实例
创建索引和数据准备 PUT course PUT course/_mapping/course { "properties": { "course":{ &qu ...
- HashMap和ConcurrentHashMap和HashTable的底层原理与剖析
HashMap 可以允许key为null,value为null,但HashMap的是线程不安全的 HashMap 底层是数组 + 链表的数据结构 在jdk 1.7 中 map集合中的每一项都是一个 ...
- ng2 学习笔记(一)
ng2发布了一段时间,最近才开始着手学习一下,ng2可以说变化海是比较大的,现在写一些学习过程中要注意的点,新手可以参考,大神可以指导: 按照文档来吧: 1.快速开始:没什么可说的,直接上git 克隆 ...
- JSON/JSONP浅谈
一.什么是JSON? JSON 即 JavaScript Object Notation 的缩写,简而言之就是JS对象的表示方法,是一种轻量级的数据交换格式. JSON 是存储和交换文本信息的语法,类 ...
- python3 on macos with vscode
brew install python3 python3 -m pip install pylint python3 -m pip install autopep8 python3 -m pip in ...
- 定位布局—position
1. position的属性 1.1position:static 默认位置,没有定位效果 1.2 position:relative 相对定位,不会脱离文档流,相对于原来位置的变化 <!DOC ...
- 删除 mac 垃圾桶内清除不掉的文件
命令行 内 $ sudo rm -rf ~/.Trash/