我有两个包含这些结构的表:
表:收入
id | Date | IncomeAmount
表:费用
id | Date | ExpenseAmount
我在一天内有多个注册表,所以我需要在白天添加字段IncomeAmount
和ExpenseAmount
并显示如下内容:
Date | IncomeAmount | ExpenseAmount | Balance
2018-01-01 | 10 | 5 | 5
2018-01-02 | 0 | 5 | -5
2018-01-03 | 20 | 0 | 20
如示例中所示,可能在某些天没有为IncomeAmount或ExpenseAmount字段注册。
非常感谢任何有关如何实现这一目标的建议。
对不起我的英语。 提前谢谢。
答案 0 :(得分:0)
如果您收到新收入运行此查询并更新您的每日表:(您还没有提到表名,所以只需更改它)
$today = \Carbon\Carbon::now()->format('Y-m-d');
Income::insert([
'Date' => $today,
'IncomeAmount' => $income
]);
$today_report = ReportTable::where(['date', $today])->get()[0];
$today_report->IncomeAmount = $today_report->IncomeAmount + $income;
$today_report->balance = $today_report->IncomeAmount - $today_report->ExpenseAmount;
$today_report->save();
如果你收到新的费用:
$today = \Carbon\Carbon::now()->format('Y-m-d');
Expense::insert([
'Date' => $today,
'ExpenseAmount' => $expense
]);
$today_report = ReportTable::where(['date', $today])->get()[0];
$today_report->ExpenseAmount = $today_report->ExpenseAmount + $income;
$today_report->balance = $today_report->IncomeAmount - $today_report->ExpenseAmount;
$today_report->save();
我认为这就是你所需要的。 注意有模型
Income, Expense, ReportTable
编辑编辑编辑
$incomes = Income::select('*', DB::raw('SUM(incomeAmount) as incomeAmount'))
->groupBy('date')
->get()
->pluck('incomeAmount','date');
$expenses = Expense::select('*', DB::raw('SUM(expenseAmount) as expenseAmount'))
->groupBy('date')
->get()
->pluck('expenseAmount','date');
$day_balance = $income['2018-15-06'] - $income['2018-15-06'];