douchui4459 2018-03-12 19:30
浏览 917

laravel eloquent的分组查询结果总和

I have two tables one transactions and another transaction_charges each transaction might have multiple transaction charges means an has many relationships. now what I need is to get transactions that occured in a month/year/week. it would be better if I can do a group by inside the query itself rather than doing a loop with the model collections.

so far I have written code to fetch the transactions with its total charge

 $defaults = collect([]);
    /** @var Builder $transactions */
    $transactions = app(TransactionServices::class)
        ->getTransactions($defaults->merge($options))
        ->has('charges')->has('operation')->selectSub(function ($query){
            /** @var Builder $query */
            $query->selectRaw('SUM(amount)')->from('transaction_charges tc')
                ->whereRaw('tc.transaction_id = transaction.transaction_id');
        }, 'totalCharge');

but to get the monthly result I need to do a group by but I don't think the group by will fetch correct aggregated result from the subquery as here what I need is the sum of the totalCharge in a month/week/group. so how I can get the transactions with its charges sum up to the monthly/Yearly group

  • 写回答

1条回答 默认 最新

  • dongzhi4073 2018-03-12 20:12
    关注

    I don't know well how your system works but I did an intent of understand it and think one possible solution for your problem.
    I made my best effort to explain the code. I Hope you understand that english is not my native language.

    // I suppose that TransactionServices represents "transaction" Table and has transaction_charges
    $transactions = TransactionServices::has('charges')->get(); // If I understand well your code that returns the transactions that has charges.
    // $transactions = TransactionServices::has('charges')->has('operation')->get(); // that returns the transactions that has charges AND operation.
    
    // the month to filter the charges
    $month = 'march'; // selected month can be dynamic this is an example
    
    // Get all TransactionServices with total amount of charges
    $transactionsWithTotalCharge = $transactions->each(function ($transaction, $key) {
        $totalAmount = $transaction->charges->where('month', $month)->sum('amount'); // This make the sum of all charges of march for that transaction 
        $transaction->put('chargesTotalAmount' , $totalAmount); // This add chargesTotalAmount to every TransactionServices
        return $transaction;
    });
    
    // You can verify if the sum is correct
    foreach($transactionsWithTotalCharge as $transaction) {
        // dd($transaction); 
        dd($transaction->chargesTotalAmount); 
    }
    

    I could not test that and hope that work

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False