【发布时间】:2021-03-14 08:11:15
【问题描述】:
我的 Laravel 项目中有四个表。有“资产”、“收入”、“费用”、“所有者权益”。每个表都有一些共同的列:
id, ref, transaction_type, payment_type, amount, transaction_date
我想要来自所有具有公共列where payment_type = Cash 的表的数据。我希望它按transaction_date 升序或降序排列。
如何在我的 laravel 8.12 中为此编写查询?
public function getDayBook()
{
$expenses = DB::table('expenses')->where('payment_type', 'Cash')->select('expense_date as date', 'ref', 'description', 'amount');
$asset = DB::table('assets')->where('payment_type', 'Cash')->select('asset_date as date', 'ref', 'description', 'amount');
$liablility = DB::table('liabilities')->where('payment_type', 'Cash')->select('liability_date as date', 'ref', 'description', 'amount');
$equity=DB::table('ownerequities')->where('payment_type','Cash')->select('equity_date as date','ref','description','amount');
$query=DB::table('incomes')
->select('income_date as date','ref','description','amount')->where('payment_type','Cash')
->union($expenses)
->union($asset)
->union($liablility)
->union($equity)
->orderBy('date','DESC');
return view('daybook.cash')->with('data', $query);
}
【问题讨论】:
-
请不要在您的标题中添加
(Solve)。如果您有解决方案,请将其发布在下面的答案部分。 -
您提供的当前代码的具体问题是什么?
标签: php mysql database laravel-8