当需要条件查找时,可以使用下面的注入方法:
//我要预约 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' => '未查到医生!']];
} }