[英]How to solve Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous
Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous 完整性约束违规:1052 where子句中的'agent_id'列不明确
I have try but still can't find the error of this datatables 我试过但仍然找不到这个数据表的错误
public function customerOrderList(Request $request, $agent_id){
$customer_orders = CustomerOrder::join('agent as a', 'a.agent_id','=','customer_order.agent_id')
->select('customer_order.*', 'a.name as agent_name')
->where('agent_id', $agent_id)
->get();
$datatables = DataTables::of($customer_orders)
->addColumn('actions', function($customer_order){
$html ='';
$view = route('customer-order.invoice', $customer_order->doc_id);
$html .= "<a class='btn btn-primary btn-sm' href='$view'><i class='far fa-fw fa-eye'></i></a>";
return $html;
})
->rawColumns(['actions']);
return $datatables->make(true);
}
-show datatable - 显示数据表
It means that multiple tables in your query have the same agent_id
column, and your query cannot determine which one to use. 这意味着查询中的多个表具有相同的agent_id
列,并且您的查询无法确定要使用哪个表。 You've used 'customer_order.agent_id'
in one clause, but only 'agent_id'
in the another. 您在一个子句中使用了'customer_order.agent_id'
,但在另一个子句中只使用了'agent_id'
。
When using ->join()
, it's sometimes required to be as specific as possible with your references (depends on your table structure/columns being selected, etc): 使用->join()
,有时需要使用引用尽可能具体(取决于您选择的表结构/列等):
$customer_orders = CustomerOrder::join('agent as a', 'a.agent_id','=','customer_order.agent_id')
->select('customer_order.*', 'a.name as agent_name')
->where('customer_order.agent_id', $agent_id) // Here, add table name before `agent_id`, likely `customer_order`
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.