如何解决违反完整性约束的问题:where子句不明确的1052列'agent_id' [英] How to solve Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous

查看:61
本文介绍了如何解决违反完整性约束的问题:where子句不明确的1052列'agent_id'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

违反完整性约束:1052 where子句中的列"agent_id"不明确

Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous

我已经尝试过,但仍然找不到此数据表的错误

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

-显示数据表

推荐答案

这意味着查询中的多个表具有相同的 agent_id 列,而您的查询无法确定要使用哪个表.您在一个子句中使用了'customer_order.agent_id',但在另一子句中仅使用了'agent_id'.

It means that multiple tables in your query have the same agent_id column, and your query cannot determine which one to use. You've used 'customer_order.agent_id' in one clause, but only 'agent_id' in the another.

使用-> join()时,有时需要对您的引用尽可能具体(取决于您的表结构/所选择的列等):

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):

$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();

这篇关于如何解决违反完整性约束的问题:where子句不明确的1052列'agent_id'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆