我的控制器中有以下内容:
$documents = $equipment->attachments()
->where('attachmentCategory','=','Document')
->orWhere('attachmentCategory','=','Vehicle Title')
->get();
但是,尽管具有等于Document
的attachmentCategory
的文档返回得很好,但它继续引入具有Vehicle Title
attachmentCategory的所有文档,即使我仅使用where和它为Vehicle Title
来尝试此语句,它也将只返回一个文档(这是正确的)。
这是我的函数中定义的关系,在其他地方也没有失败,所以我知道它在上面的控制器语句中,我只是把它放在这里以防万一。
public function attachments()
{
return $this->hasMany(EquipmentAttachment::class,'unitID','id');
}
如果你能纠正上面的陈述,那就太好了。我希望只有当他们的attachmentCategory为Document
或Vehicle Title
时,它才会将文档返回给我。
发布于 2018-06-03 11:48:12
看起来这可能是Laravel的bug。我来测试一下。
这就是解决方案:
$documents = $equipment->attachments()
->where(function ($q) {
$q
->where('attachmentCategory','=','Document')
->orWhere('attachmentCategory','=','Vehicle Title');
})
->get();
https://stackoverflow.com/questions/50663061
复制相似问题