我有三张桌子。 work_orders
,customers
和aircraft
。在work_orders
表中,有两个字段customer_id
和aircraft_id
。我试图通过customers
检索aircraft
和join
数据。我得到了一系列错误,但它们似乎都指向了同一个方向,那就是无法找到表。
这是我的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_orders
。work_orders
work
aircraft
id
。work
= {{1 } {。{1}}内部加入aircraft_id
work_orders
workorder
。workorder
=customer_id
。customers
)
我已尝试将id
与work.aircraft_id
切换,因为Laravel文档按顺序显示,但这并没有任何区别。我能摆脱这个错误的唯一方法是删除我的aircraft.id
语句。
答案 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'));
}