Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

akhilmurali's avatar

How can I show transaction details bar chart?

How can i show total transaction group by month in chart js? I have a code where i can show total no of transactions in month. But I need total sum of transactions. Below is the code:

 $pie_data = order::select('total_amount' ,'created_at')->get()->groupBy(function($pie_data){
            return Carbon::parse($pie_data->created_at)->format('M');
        });

        $piemonths = []; 
        $piemonthTrans= []; 
        foreach($pie_data as $piemonth => $values){
            $piemonths[] = $piemonth;
            $piemonthTrans[] = count($values);
            
        }

When i change count function with sum , it is showing error - "Call to undefined function App\Http\Controllers\Admin\sum()"

0 likes
1 reply
Snapey's avatar

so, bear in mind that you are only grouping by month so next year and this years data will be lumped together. You need to ground by month and year

You are also loading every order into memory

Loop values, adding to total or convert to collection and use ->sum('total_anount')

better, use aggregate functions in the database

Please or to participate in this conversation.