0

I'm new in 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.

5
  • you cannot join two table in one closure. Commented Mar 16, 2020 at 3:06
  • @TsaiKoga which mean i need to join the others two table as sub query? Commented Mar 16, 2020 at 3:22
  • payment_methods and transactables ? Commented Mar 16, 2020 at 3:29
  • @TsaiKoga Yes. Or do you have any suggestion? Commented Mar 16, 2020 at 3:45
  • Use multiple leftjoin, or you can call eloquent builder relationship by with. Commented Mar 16, 2020 at 4:02

1 Answer 1

1

Why don't you use the Eloquent like

$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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.