未找到列:1054未知列 - Laravel 5.1加入

时间:2016-01-22 21:06:22

标签: php mysql laravel-5.1

我有三张桌子。 work_orderscustomersaircraft。在work_orders表中,有两个字段customer_idaircraft_id。我试图通过customers检索aircraftjoin数据。我得到了一系列错误,但它们似乎都指向了同一个方向,那就是无法找到表。

这是我的WorkOrderController index

public function index(WorkOrder $workorder)
{
$workorder_array = $workorder
    ->join('work_orders as work', 'work.aircraft_id', '=', 'aircraft.id')
    ->join('work_orders as workorder', 'workorder.customer_id', '=', 'customers.id')
    ->select('work_orders.opened_date', 'customers.mobile', 'aircraft.year')
    ->get();

return view('work-orders.index', compact('workorder_array'));
}

由此我得到以下错误:

  

SQLSTATE [42S22]:未找到列:1054未知列' aircraft.id'在' on条款' (SQL:从work_orders opened_date选择work_orderswork_orders work aircraft idwork = {{1 } {。{1}}内部加入aircraft_id work_orders workorderworkorder = customer_idcustomers

我已尝试将idwork.aircraft_id切换,因为Laravel文档按顺序显示,但这并没有任何区别。我能摆脱这个错误的唯一方法是删除我的aircraft.id语句。

1 个答案:

答案 0 :(得分:0)

join声明中有错误的表格。这就是我应该拥有的:

public function index(WorkOrder $workorder)
{
    $workorder_array = $workorder
        ->join('aircraft', 'work_orders.aircraft_id', '=', 'aircraft.id')
        ->join('customers', 'work_orders.customer_id', '=', 'customers.id')
        ->select('*')
        ->get();

    return view('work-orders.index', compact('workorder_array'));
}