【问题标题】:How to solve Integrity constraint violation: 1052 Column 'agent_id' in where clause is ambiguous如何解决违反完整性约束:where 子句中的 1052 列“agent_id”不明确
【发布时间】:2019-05-06 22:35:59
【问题描述】:

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

我试过了,还是找不到这个数据表的错误

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

-显示数据表

【问题讨论】:

    标签: laravel datatable


    【解决方案1】:

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

    使用-&gt;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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2019-10-03
      • 2022-11-24
      • 1970-01-01
      • 2013-10-16
      • 2020-10-13
      • 2019-04-14
      相关资源
      最近更新 更多