在 laravel 中获得每月总和
get monthly sum in laravel
我在 laravel 中有两个数组,包含当年和上一年每个月的第一天。
$current_month = new \Carbon\Carbon('first day of this month');
$prevyear_month = new \Carbon\Carbon('first day of this month last year');
$current_year = array();
$prev_year = array();
array_push($prev_year, $prevyear_month->toDateString());
array_push($current_year, $current_month->toDateString());
for($i = 1; $i<12; $i++)
{
$temp = $current_month->addMonth();
$temp_prev = $prevyear_month->addMonth();
array_push($current_year, $temp->toDateString());
array_push($prev_year, $temp_prev->toDateString());
}
这是结果
Array ( [0] => 2016-03-01 [1] => 2016-04-01 [2] => 2016-05-01 [3] => 2016-06-01 [4] => 2016-07-01 [5] => 2016-08-01 [6] => 2016-09-01 [7] => 2016-10-01 [8] => 2016-11-01 [9] => 2016-12-01 [10] => 2017-01-01 [11] => 2017-02-01 )
Array ( [0] => 2015-03-01 [1] => 2015-04-01 [2] => 2015-05-01 [3] => 2015-06-01 [4] => 2015-07-01 [5] => 2015-08-01 [6] => 2015-09-01 [7] => 2015-10-01 [8] => 2015-11-01 [9] => 2015-12-01 [10] => 2016-01-01 [11] => 2016-02-01 )
我正在尝试获取所有这些月份的总和,但不知道如何去做。如果这是要走的路,我可以做原始查询。
Table 每一天都有行。带有日期栏(碳样式 ex. 2016-01-01)和收入栏。我试图在上面的数组中获取每个月的收入总和。
您需要稍微清理一下,但这是完成此操作的良好开端。
以下是我刚刚编写的示例函数,您可以修改并添加到您的模型中:
public function getMonthlySum(Carbon $date)
{
$year = $date->year;
$month = $date->month;
if ($month < 10) {
$month = '0' . $month;
}
$search = $year . '-' . $month;
$revenues = parent::where('date', 'like', $search .'%')->get();
$sum = 0;
foreach ($revenues as $revenue) {
$sum += $revenue->revenue;
}
return $sum;
}
这将计算 table 中与作为参数传递的 $date 具有相同年份和月份的任何条目的收入总和。
您应该能够使它适应您的代码,并针对您想要计算的任何其他总和稍微调整它,例如 getYearlySum
.
我在 laravel 中有两个数组,包含当年和上一年每个月的第一天。
$current_month = new \Carbon\Carbon('first day of this month');
$prevyear_month = new \Carbon\Carbon('first day of this month last year');
$current_year = array();
$prev_year = array();
array_push($prev_year, $prevyear_month->toDateString());
array_push($current_year, $current_month->toDateString());
for($i = 1; $i<12; $i++)
{
$temp = $current_month->addMonth();
$temp_prev = $prevyear_month->addMonth();
array_push($current_year, $temp->toDateString());
array_push($prev_year, $temp_prev->toDateString());
}
这是结果
Array ( [0] => 2016-03-01 [1] => 2016-04-01 [2] => 2016-05-01 [3] => 2016-06-01 [4] => 2016-07-01 [5] => 2016-08-01 [6] => 2016-09-01 [7] => 2016-10-01 [8] => 2016-11-01 [9] => 2016-12-01 [10] => 2017-01-01 [11] => 2017-02-01 )
Array ( [0] => 2015-03-01 [1] => 2015-04-01 [2] => 2015-05-01 [3] => 2015-06-01 [4] => 2015-07-01 [5] => 2015-08-01 [6] => 2015-09-01 [7] => 2015-10-01 [8] => 2015-11-01 [9] => 2015-12-01 [10] => 2016-01-01 [11] => 2016-02-01 )
我正在尝试获取所有这些月份的总和,但不知道如何去做。如果这是要走的路,我可以做原始查询。
Table 每一天都有行。带有日期栏(碳样式 ex. 2016-01-01)和收入栏。我试图在上面的数组中获取每个月的收入总和。
您需要稍微清理一下,但这是完成此操作的良好开端。
以下是我刚刚编写的示例函数,您可以修改并添加到您的模型中:
public function getMonthlySum(Carbon $date)
{
$year = $date->year;
$month = $date->month;
if ($month < 10) {
$month = '0' . $month;
}
$search = $year . '-' . $month;
$revenues = parent::where('date', 'like', $search .'%')->get();
$sum = 0;
foreach ($revenues as $revenue) {
$sum += $revenue->revenue;
}
return $sum;
}
这将计算 table 中与作为参数传递的 $date 具有相同年份和月份的任何条目的收入总和。
您应该能够使它适应您的代码,并针对您想要计算的任何其他总和稍微调整它,例如 getYearlySum
.