【Laravel】DB查询 where 例子记录

在数据查询时候,多条件查询,使用场景

//单个值
$data_where['id'] = 1

// in 条件 写法一
$ids = [1,2,3,4,5];
$data_where[] = [DB::raw("字段名 in ({$ids})"),'1'];
//in条件写法二
$data_where[] = ['in'=>['id'=>$ids]];

$condition[] =['id','in',$ids]; // 这是错误的写法
// IlluminateDatabaseQueryBuilder关于operators定义中,并没有in
public $operators = [
    '=', '<', '>', '<=', '>=', '<>', '!=',
    'like', 'like binary', 'not like', 'between', 'ilike',
    '&', '|', '^', '<<', '>>',
    'rlike', 'regexp', 'not regexp',
    '~', '~*', '!~', '!~*', 'similar to',
    'not similar to', 'not ilike', '~~*', '!~~*',
];


经测试,上述方法 in 的时候 无法使用

后改为如下实现:

 $check = Db::table('check as c')
->leftJoin('report as  r','r.check_code','=','c.num')
->where(function ($query) use ($user_id,$user_info,$param) {
     //场景一: where 的 或条件
      $query->where(function ($query) use ($name) {
           $query->where('pt.name', '=', $name)->orWhere('p.name', '=', $name);
      });      
     //场景二: in条件
     $query->whereIn('o.scan_doctor_id', [1,2,3]);

})->orderBy('c.update_time','desc')
->select(['r.id'])
->paginate(10);

原文地址:https://www.cnblogs.com/richerdyoung/p/13836648.html