1. 程式人生 > >laravel 中條件查詢 function模式

laravel 中條件查詢 function模式

where enc use size == 條件查找 work file nts

當需要條件查找時,可以使用下面的註入方法:

//我要預約 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模式