如何通过整月显示来查找特定年份

时间:2019-04-18 07:18:07

标签: php laravel date

1,我可以获得当年的收入,但不知道如何显示当年的所有月收入。

2,如果我要创建 $ selectYear 作为随机年份之一,请显示特定年份的月收入 $ selectYear 就像问题1一样。

我只尝试了当前/选定年份的收入,但显示了年度的月收入,我仍然停留在那儿。

这是我的控制器

public function index(){
  $selectYear= 2017;
    $this->getYearRevenue($selectYear);
    //customPickYearRevenue is for totalIncome of $selectYear
    $customPickYearRevenue = $this->getYearRevenue($selectYear);
}

private function getYearRevenue($selectYear){
    $yearPickRevenue = Order::whereYear('created_at', '=', $selectYear)
    ->where('order_status', 'Accepted')
    ->sum('amount');

    return $yearPickRevenue;
  }

我要显示金额(月份收入)的月份

对不起,硬编码 $ selectYear

1 个答案:

答案 0 :(得分:0)

也许是这样的:

private function getRevenue($selectYear, $selectMonth){
        return Order::whereYear('created_at', '=', $selectYear)
            ->whereMonth('created_at', '=', $selectMonth)
            ->where('order_status', 'Accepted')
            ->sum('amount');
    }

然后您可以使用循环获取每个月的收入:

$year = 20019;
$months = ['1', '2', '3',];
$revenues = [];

foreach($months as $month){
     array_push($revenues, getRevenue($year, $month);
}