简体   繁体   English

如何使用Laravel查询构建器合并这两个函数

[英]How to merge these two function with Laravel query builder

I need to export final amount after calculation of all payable and receivable of a member now I do in two function how to merge them 我需要在计算成员的所有应付款和应收款之后导出最终金额,现在我在两个函数中执行如何将它们合并的操作

public function memberReceivable($id){

    return $this->join('transactions', 'transactions.id', '=', 'member_transactions.transaction_id')
    ->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
    ->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount")
    ->where('accounts.type','accountsReceivable')
    ->where('member_transactions.member_id',$id)
    ->groupBy('transactions.unit')->get();    
}

public function memberPayable($id){

    return $this->join('transactions', 'transactions.id', '=', 'member_transactions.transaction_id')
             ->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
             ->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount")
             ->where('accounts.type','accountsPayable')
             ->where('member_transactions.member_id',$id)
             ->groupBy('transactions.unit')->get();
}
public function member($id = null, $type = ""){
    $response = $this->join('transactions', 'transactions.id', '=', 
    'member_transactions.transaction_id')
    ->leftjoin('accounts', 'accounts.id', '=', 'transactions.account_id')
    ->selectRaw("transactions.unit as currency,sum(transactions.amount) as amount");

    if($type){
      $response->where('accounts.type', $type);
    }

    if($id){
      $response->where('member_transactions.member_id',$id);
    }

    $response->groupBy('transactions.unit')->get();

    return $response;
}

You can try it. 你可以试试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM