[英]relations with 4 tables in laravel
I have four tables as follows.我有四个表如下。
Services_cats![]() |
Session_pat ![]() |
Invoice_item![]() |
Invoice![]() |
---|---|---|---|
id ![]() |
id ![]() |
id ![]() |
id ![]() |
name![]() |
services_cat_id ![]() |
service_id![]() |
code![]() |
patient_id![]() |
invoice_id![]() |
discount![]() |
I need to have relations among them with one Eloquent query in Laravel. Below is what I have reached so far;我需要通过 Laravel 中的一个 Eloquent 查询在它们之间建立关系。以下是我到目前为止所达到的; I used many-to-many relations in the invoice table.
我在发票表中使用了多对多关系。
public function invoice_item()
{
return $this->belongsToMany(Session_pat::class, 'invoice_items', "invoice_id",
"service_id", "id", "id");
}
And I used this code to access the four tables.我用这段代码访问了四个表。
$status4 = Invoice::select('id')->with(['invoice_item' => function ($q){
$q->select('id', 'services_cat_id')
->with(['service_cat' => function ($q) {$q->select('id as myid', 'name');}])
;}])
->where('id', 122)->get();
but I get this error但我得到这个错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select
id
,services_cat_id
,invoice_items
.invoice_id
aspivot_invoice_id
,invoice_items
.service_id
aspivot_service_id
fromsession_pats
inner joininvoice_items
onsession_pats
.id
=invoice_items
.service_id
whereinvoice_items
.invoice_id
in (122) order byid
desc)SQLSTATE[23000]:违反完整性约束:字段列表中的 1052 列“
id
”不明确(SQL:selectid
,services_cat_id
,invoice_items
作为pivot_invoice_id
,invoice_id
pivot_service_id
service_id
session_pats
内部加入invoice_items
invoice_items
session_pats
=invoice_items
。service_id
whereinvoice_items
.invoice_id
in (122) order byid
desc)
You Should Specific The Table Name, WHen Using Same Column Name In Query.当在查询中使用相同的列名时,您应该指定表名。
Rule: tableName.column EX: invoiceTable.id规则:tableName.column EX: invoiceTable.id
$status4 = Invoice::select('TableName.id')->with(['invoice_item' => function ($q){
$q->select('TableName.id', 'services_cat_id')
->with(['service_cat' => function ($q) {$q->select('TableName.id as myid', 'name');}])
;}])
->where('TableName.id', 122)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.