1
votes

My table definition is as below

**PURCHASE_PYAMENTS**

SELLER      MASTER_PAYMENT      BILL        COUNT_VB        PAYMENT_STATUS      REMAIN_PARTIAL_AMOUNT

abc         15000               0           0               1                   0   
xyz         14000               1           1               1                   14000
abc         15000               0           0               0                   5000

I need sum of master_payment column for specific seller under two condition:

  1. payment_status = 1 and count_vb = 0
  2. payment_status = 0 and remain_partial_amount > 0

If any of above condition goes true then master_payment should add in sum so, how can I achieve it using single query?.

I try it by my self but get a result for only one condition my query is as below:

  $total_count_vb = DB::table('purchase_payments')
        ->where('seller',$seller_list)
        ->where('count_vb',0)
        ->where('payment_status',1)
        ->SUM('master_payment');
1

1 Answers

0
votes

Use where() and orWhere() closures for parameter grouping:

Payment::where('seller', 'abc')->
       ->where(function ($q) {
           $q->where(function ($q) {
               $q->where('payment_status', 1)->where('count_vb', 0);
           })
           ->orWhere(function ($q) {
               $q->where('payment_status', 0)->where('remain_partial_amount', '>', 0);
           });
       });
       ->sum('master_payment');