【问题标题】:Laravel query from 4 tables来自 4 个表的 Laravel 查询
【发布时间】: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


【解决方案1】:
    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')->get();
 return view('daybook.cash')->with('data', $query);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2015-02-13
    • 2021-04-11
    相关资源
    最近更新 更多