问题描述
我有两个在多对多关系中相互关联的mongo文档.一个叫做Lawyer,另一个叫做LawCase.
I have two mongo documents that are related to each other in a many to many relationship. One is called Lawyer and the other LawCase.
我的律师模型具有:
public function cases()
{
return $this->belongsToMany('App\LawCase');
}
我的LawCase模型具有:
My LawCase model has:
public function lawyers()
{
return $this->belongsToMany('App\Lawyer');
}
我要做的就是找到具有特定类别法律案件的律师.
All I am trying to do is find lawyers that have a certain category of law cases.
$lawyers = App\Lawyer::whereHas('cases', function($q){
$q->where('category', '=', 'DUI');
})->get();
即使我的诉讼文件中包含"DUI"类别,也无法获得任何帮助.
This gets nothing even though I have lawcase documents that have a category of 'DUI'.
当我这样做
$lawyers = App\Lawyer::with('cases')->get();
那给了我一个结果集.只是在哪里有一些问题.我想念什么?
That gets me a result set. Just having some issues with wherehas. What am I missing?
我尝试研究此问题,但看起来其他人可能也有类似的问题:
I tried researching the issue but looks like others may have similar issue:
Laravel + Jenssegers \ Mongodb:哪里"和是否返回空集合
如果哪里不起作用,您将如何做呢?
If whereHas would not work, how would you get about doing this?
更新:
My Lawyer Document
{
"_id" : ObjectId("5945f88c9a89205aae0efea8"),
"full_name" : "Some Name ",
"active" : true,
"updated_at" : ISODate("2017-06-18T03:50:36.849+0000"),
"created_at" : ISODate("2017-06-18T03:50:36.849+0000"),
"law_case_ids" : [
"5945f88c9a89205aae0efea9",
"5945f88c9a89205aae0efeac",
"5945f8b59a89205aae0f3f81",
"5955d0ff9a89200a57340db8"
]
}
我的LawCase文档
My LawCase Document
{
"_id" : ObjectId("5945f88c9a89205aae0efe9a"),
"category" : "DUI",
"updated_at" : ISODate("2017-06-18T03:50:36.825+0000"),
"created_at" : ISODate("2017-06-18T03:50:36.821+0000"),
"lawyer_ids" : [
"5945f88c9a89205aae0efe99"
]
}
推荐答案
这是正确的方法,这将返回案件与类别DUI
相匹配的律师.
Here is the correct way to do it, this will return the lawyers who have cases that match the category DUI
.
$lawyers = App\Lawyer::with(['cases'=> function($q){
$q->where('category', '=', 'DUI');
}])->get();
这篇关于Laravel Mongo多对多关系无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!