在Laravel 8中尝试使用addSelect()
方法时出现以下错误
Unknown column 'contacts.contact_id' in 'where clause' (SQL: select `email_address` from
`contact_information` where `owner_id` = `contacts`.`contact_id` and `owner_type` = contact
and `contact_type` = email_address and `is_primary` = 1 limit 1)
这是我的查询
$query = Contact::addSelect(['primary_email_address' => ContactInformation::select('email_address')
->whereColumn('owner_id', 'contacts.contact_id')
->where('owner_type', 'contact')
->where('contact_type', 'email_address')
->where('is_primary', true)
->first()
])->get();
直接来自Laravel文档中的示例,如下所示:
return Destination::addSelect(['last_flight' => Flight::select('name')
->whereColumn('destination_id', 'destinations.id')
->orderBy('arrived_at', 'desc')
->limit(1)
])->get();
我已经验证了contacts
表和contact_id
列都存在。 contact_id
是联系人的主键,但是owner_id
不是contact_information
的主键。
那么,有什么用呢?是否有关于如何更正此错误并使contacts
子查询中可用的ContactInformation
表列的想法?谢谢!