laravel 查询构建器 功能非常强大,想要多功能,多参数查询问题的时候,之前一直没特别好的方法。
每次只能使用匿名函数,增加到where 里面,或者使用原生的写法。
今天记录一种,以后可以常用的查询语句。
$condition=[];
array_push($condition,['created_time','>',$start]);
array_push($condition,['created_time','<',$end]);
$condition['merchant_id']=$merchant_id;
$condition1=[];
array_push($condition1,['status','=',Order::STATUS_FINISHED,'or']);
array_push($condition1,['status','=',Order::STATUS_BALANCE,'or']);
$da=Order::where($condition)
->where($condition1)
->select(DB::raw("bank_account_id, count(bank_account_id) as
id,sum(real_amount) as num"))
->GroupBY('bank_account_id')
->orderBy('id','desc')
->get()->toArray();
laravel7以上的 还可以把select 换成selectRaw