这是我的查询代码:
$payments = DB::table('payments')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->where('payments.account_id', '=', Auth::user()->account_id)
->where('payments.is_deleted', '=', false)
->where('invoices.is_deleted', '=', false)
->where('clients.is_deleted', '=', false)
->where('contacts.deleted_at', '=', null);
当我尝试显示此查询的结果时,它给了我:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'payments_invoices.invoice_id' in 'on clause' (SQL: select `payments`.`payment_date`, `payments`.`amount`, `invoices`.`public_id`, `invoices`.`invoice_number`, `clients`.`name` as `client_name`, `contacts`.`email`, `contacts`.`first_name`, `contacts`.`last_name`, `clients`.`currency_id`, `clients`.`public_id` as `client_public_id`, `clients`.`user_id` as `client_user_id` from `payments` inner join `invoices` on `invoices`.`id` = `payments_invoices`.`invoice_id` inner join `payments_invoices` on `payments`.`id` = `payments`.`payment_id` left join `clients` on `clients`.`id` = `payments`.`client_id` left join `contacts` on `contacts`.`client_id` = `clients`.`id` where `payments`.`account_id` = 1 and `payments`.`is_deleted` = 0 and `invoices`.`is_deleted` = 0 and `clients`.`is_deleted` = 0 and `contacts`.`deleted_at` is null and `contacts`.`is_primary` = 1 order by `payments`.`payment_date` desc limit 50)
知道此查询正常运行:
$query = DB::table('payments')
->join('accounts', 'accounts.id', '=', 'payments.account_id')
->join('clients', 'clients.id', '=', 'payments.client_id')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
})
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
->where('clients.is_deleted', '=', false)
->where('payments.is_deleted', '=', false)
->where('invitations.deleted_at', '=', null)
->where('invoices.deleted_at', '=', null)
->where('invitations.contact_id', '=', $contactId)
);
我该怎么办?
答案 0 :(得分:1)
这一行
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
没有意义。你尝试使用表格中的字段加入另一个表。所以你可能想加入payments_invoices.payment_id
然后您可以尝试更改加入表格的顺序
$payments = DB::table('payments')
->join('payments_invoices', 'payments.id', '=', 'payments_invoices.payment_id')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->leftJoin('clients', 'clients.id', '=', 'payments.client_id')
->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id')
->where('payments.account_id', '=', Auth::user()->account_id)
->where('payments.is_deleted', '=', false)
->where('invoices.is_deleted', '=', false)
->where('clients.is_deleted', '=', false)
->where('contacts.deleted_at', '=', null);
答案 1 :(得分:0)
根据您的查询:
$payments = DB::table('payments')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
->join('payments_invoices', 'payments.id', '=', 'payments.payment_id')
您正在将payments
表加入invoices
表。您的on子句如下所示:ON invoices.id = payments_invoices.invoice_id
但payments_invoices
表仅在下一行加入。
只需更正payments
和invoices
表格之间的关系(如果有),那就好了。
由于payments
和invoices
是多对多的,所以请尝试以下方法:
DB::table('payments')
->join('payments_invoices', 'payments.id', '=', 'payments_invoices.payment_id')
->join('invoices', 'invoices.id', '=', 'payments_invoices.invoice_id')
如果payments_id
表上有payments_invoices
列。