在laravel中执行复杂的联接和子查询

时间:2018-06-21 11:04:21

标签: php laravel

我想生成几乎在8个表中的所有成员语句,例如 1.付款 2.贷款 3.购买 4.接收

目前想按其类型生成报告,我对所有这些函数都使用了单独的函数,因此可以在一个查询中进行操作。请考虑我要这样的报告 example

这是我的表格,我没有显示详细信息,但实际数据库几乎相同 database structure

    public function exportAllPayments($member_id=0,$type='',$from_date='',$to_date='')
{
      $query=$this->leftjoin('members','members.id','payments.member_id')
                ->leftjoin('transactions','payments.payable_transaction','transactions.id')
                ->leftjoin('accounts','transactions.account_id','accounts.id')
                ->selectRaw("payments.id as id,members.name as Name,payments.rate as Rate,payments.discount as Discount,payments.type as Payment_type,payments.description as Description, transactions.amount*-1 as amount,transactions.unit as currency,accounts.name as Account,date_format(from_unixtime(payments.date),'%b %d, %Y') as Date")
                ->orderBy('members.id');
     if($type && $type !='')
     $query =$query->where('payments.type',$type);

    if($member_id && $member_id !=0)
     $query =$query->where('members.id',$member_id);

    if($from_date && $from_date !='')
     $query =$query->where('payments.date','>=',$from_date);

     if($to_date && $to_date !='')
     $query =$query->where('payments.date','<=',$to_date);


     return $query->get();
}
     $payments = $this->payment->exportAllPayments($member->id,'',$from_date,$to_date);
           foreach ($payments as $key => $payment) {
            array_push($data, [
            'type'              =>'pay',
            'member_name'       =>$member->name,
            'Payment_type'      =>$payment->Payment_type,
            'Amount'            =>$payment->amount,
            'currency'          =>$payment->currency,
            'Rate'              =>$payment->Rate,
            'Account'           =>$payment->Account,
            'Date'              =>$payment->Date
            ]); 

        }

0 个答案:

没有答案