laravel 不使用匿名函數 實現where or 和count sum等功能

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章