php - laravel怎么优雅的拼接where,处理whereIn与where数组查询的问题

查看:103
本文介绍了php - laravel怎么优雅的拼接where,处理whereIn与where数组查询的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

现在我要处理多条件搜索的问题

我的代码是这么处理的

  switch ($where['type']) {
                //基层医院姓名
                case 1:
                    $condition [] = ['create_doctor_hospital', 'like', $where['name'] . "%"];
                    break;
                //基层医生姓名
                case 2:
                    $condition [] = ['create_doctor_name', 'like', $where['name'] . "%"];
                    break;
                //上级医生姓名
                case 3:
                    $doctor_model=UserModel::select('doctoruid')
                        ->where('doctorname', 'like', $where['name'] . "%")
                        ->get('doctoruid');
                    //todo:会存在匹配到多个叫刘江淮的医生 结果集要怎么处理
                    if(empty($doctor_model)){
                        break;
                    }else{
                        foreach ($doctor_model as $doc){
                            $check_doctor_id[]=$doc->doctoruid;
                        }
                    }
                    $condition[] =['check_doctor_uid','in',$check_doctor_id];
                    break;
            }
        }


       if (isset($where['start_time']) && isset($where['end_time'])) {
            $order_data = $this
                ->join(HDW_CHECKREPORT_ECG, $this->table . '.', '=', HDW_CHECKREPORT_ECG . '.id')
                ->select(HDW_CHECKREPORT_ECG . '.check_time', HDW_CHECKREPORT_ECG . '.patient_name', HDW_CHECKREPORT_ECG . '.patient_mobile', HDW_CHECKREPORT_ECG . '.patient_idcard', $this->table . '.orderid', $this->table . '.pay_money')
                ->whereBetween(HDW_ORDER . '.created_at', [$where['start_time'], $where['end_time'] + 24 * 60 * 60])
                ->where($condition)
                ->get();
        } else {
            //todo:sql待测试
            $order_data = $this
                ->join('doctor_user', $this->table . '.check_doctor_uid', '=', 'doctor_user.doctoruid')
                ->join(HDW_PRODUCT, $this->table . '.product_id', '=', HDW_PRODUCT . '.productid')
                ->select($this->table . '.orderid', $this->table . '.patient_name', $this->table . '.product_id', $this->table . '.create_doctor_uid', $this->table . '.pay_money', $this->table . '.create_doctor_hospital', $this->table . '.check_doctor_uid', $this->table . '.create_doctor_name', 'doctor_user.doctorname', HDW_PRODUCT . '.name')
                ->where($condition)
                ->get();
        }
        
       由于前几个条件都是拼接数组 作为where条件传入的 在搜索上级医生的时候 由于会匹配到多个医生 所以需要使用到wherein   但是不知道怎么去拼接whereIn  或者  作为数组传进去  求sg大神解惑

解决方案

你的代码有错误:

//in查询应该用whereIn
$condition[] =['check_doctor_uid','in',$check_doctor_id]; // 错误
// Illuminate\Database\Query\Builder关于operators定义中,并没有in
public $operators = [
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'like binary', 'not like', 'between', 'ilike',
    '&', '|', '^', '<<', '>>',
    'rlike', 'regexp', 'not regexp',
    '~', '~*', '!~', '!~*', 'similar to',
    'not similar to', 'not ilike', '~~*', '!~~*',
];


//->where($condition) 这种写法有问题

链式调用也可以不一次写完的,参考代码

$query = DB::table('orders')->select('department', DB::raw('SUM(price) as total_sales'));
if(...){
    $query = ...
}
$query = $query->groupBy('department')->havingRaw('SUM(price) > 2500');
$orders = $query->get();

如果是5.4版本,可以使用when方法,参考https://segmentfault.com/a/11...

这篇关于php - laravel怎么优雅的拼接where,处理whereIn与where数组查询的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆