如何解决违反完整性约束的问题:1052 where子句中的列“ agent_id”不明确

时间:2019-05-06 14:30:55

标签: laravel datatable

违反完整性约束: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);
  }

-显示数据表

1 个答案:

答案 0 :(得分:0)

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

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