如何获取当前年份的每月金额总额并将其显示在consoleTv条形图中

时间:2019-06-15 14:33:06

标签: mysql laravel eloquent

我尝试了很少的查询。但这对我不起作用。

$amount = DB::select('select month(created_at) as month, 
sum(amount) as total_amount 
from orders 
group by month(created_at)');

我正在获得类似的价值

array:2 [▼
  0 => {#2186 ▼
    +"month": 5
    +"total_amount": 720.0
  }
  1 => {#2187 ▼
    +"month": 6
    +"total_amount": 720.0
  }
]

但是在consoleTv中使用此数据时会出错。

错误:在数组上调用成员函数filter()

1 个答案:

答案 0 :(得分:0)

您可以使用此代码。

$data = orders::select(
                \DB::raw('MONTH(created_at) as months'),
                \DB::raw("Sum(amount) as sums")
            )
            ->groupBy('months')
            ->orderBy('months','asc')
            ->get();

        $chart = Charts::database($data, 'bar', 'highcharts')
            ->title("Monthly Amount")
            ->elementLabel("Total Amount")
            ->dimensions(300, 500)
            ->responsive(false)
			->groupBy('months')
			->values($data->pluck('sums'));