Friday, March 3, 2017

How to sum value by month and assign two different variables?

I wish to sum up payment_amount by created_at date and assign the total by month to two different variables so I can plot on charts js x and y axis.

So far i have output json string using the below code.

public function index()
{

    $data = [
        'period' => Payment::all('created_at', 'payment_amount')->groupBy(function($date) {
             return Carbon::parse($date->created_at)->format('F');
        })->tojson(),
    ];

    //dd($data);
    return view('admin.dashboard', $data);  
}   

so to one json string I wish to have $created_at //which goes on the x axis $total payment_amount //which goes on y axis.

Here is the charts js part i need to set the variables...

var barChartData = {
    labels: <?php echo $created_at; ?>, //by month.
    datasets: [{
        label: 'Revenue',
        backgroundColor: "rgba(220,220,220,0.5)",
        data:  <?php echo $payment_amount; ?>, //summed up by month
    }]
};



via Eden WebStudio

Advertisement