How to get sum of ‘payments’ per day in rage of last 30 days in Laravel

  laravel, php, php-carbon

I have ‘amount’ row in the payment database and I want to return sum of all payments per day in the last 30 days but i got stuck a little bit. 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();
    }

Source: Laravel

Leave a Reply