[英]Laravel/Eloquent: replace with() for leftjoin() or join()
[英]Laravel eloquent join multiple tables in the leftjoin
我是 Laravel 的新手。 我試圖在左連接中加入多個表,但是我面臨語法錯誤,我不知道哪里出了問題。
代碼
$query = DB::table('sales')
->leftjoin('transactions AS trx', function ($join) {
$join->on('payment_methods AS payment', 'payment.id', '=', 'trx.payment_method_id');
$join->on('transactables', 'transactables.transaction_id', '=', 'transactions.id')
->whereNull('transactions.deleted_at')
->whereNull('transactables.deleted_at')
->where('transactable_type', '=', 'Sale')
->where('transactable_id', '=', 'sales.id');
})
錯誤信息
Syntax error near '`payment_methods` as `payment` payment.id `=` and `transactions`.`deleted_at` is'
正如上面的代碼你可以看到。 我正在嘗試在transactions
表中加入表payment_methods
和transactables
。
你為什么不使用 Eloquent 之類的
$sales = Sale::with('transactions')->get();
在銷售模型中添加關系
public function transactions()
{
return $this->morphToMany('App\Transaction', 'transactable');
}
有關更多信息,請參閱Laravel 文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.