【问题标题】:How to get sum of 'payments' per day in rage of last 30 days in Laravel [duplicate]如何在 Laravel 过去 30 天的愤怒中每天获得“付款”的总和 [重复]
【发布时间】:2021-02-23 18:45:18
【问题描述】:

我在付款数据库中有 'amount' 行,我想返回过去 30 天内每天所有付款的总和,但我有点卡住了。 This is what im expecting

    $start = Carbon::now()->addDays(-30);
    $end = Carbon::now();

    $paymentslast30days = Payment::where('payment_date','<', $end)->where('payment_date','>',$start)->get();


    $days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

    foreach($days as $day)
    {
     $day_date = $start->addDays($day-1);
     $number_of_payment_in_day = $paymentslast30days->where('payment_date',$day_date)->count();
    }

【问题讨论】:

    标签: php laravel php-carbon


    【解决方案1】:

    Here's the docs regarding grouping.

    Payment::groupBy('payment_date')
        ->selectRaw('payment_date, SUM(amount)')
        ->whereRaw('payment_date >= DATE(NOW()) - INTERVAL 30 DAY)
        ->orderByDesc('payment_date')
    

    两件事:

    • 我不确定你的 amount 字段叫什么
    • 日期的 where 子句是 MySQL 语法,您可能需要针对不同的数据库引擎进行调整

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 2012-12-09
      • 2022-01-09
      • 2022-01-18
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多