Laravel雄辩的雄辩addSelect()父查询中的父列-该列不存在

时间:2020-11-10 02:37:28

标签: php mysql laravel eloquent laravel-8

在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表列的想法?谢谢!

0 个答案:

没有答案
相关问题