简体   繁体   English

Laravel eloquent join 多个表的leftjoin

[英]Laravel eloquent join multiple tables in the leftjoin

I'm new in laravel.我是 Laravel 的新手。 I'm trying to join multiple tables in the left join however i facing the syntax error and i have no idea where goes wrong.我试图在左连接中加入多个表,但是我面临语法错误,我不知道哪里出了问题。

Code代码

$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');
    })

The error meesage错误信息

Syntax error near '`payment_methods` as `payment` payment.id `=` and `transactions`.`deleted_at` is'

As the code above you can see.正如上面的代码你可以看到。 I'm trying to join table payment_methods and transactables within the transactions table.我正在尝试在transactions表中加入表payment_methodstransactables

Why don't you use the Eloquent like你为什么不使用 Eloquent 之类的

$sales = Sale::with('transactions')->get();

Add relation in Sale model在销售模型中添加关系

public function transactions()
{
    return $this->morphToMany('App\Transaction', 'transactable');
}

For more info, please refer to Laravel docs有关更多信息,请参阅Laravel 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM