7

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.

My Lawyer model has:

public function cases()
    {
        return $this->belongsToMany('App\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();

This gets nothing even though I have lawcase documents that have a category of 'DUI'.

When I do

$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: 'WhereHas' and 'Has' returns empty collection

If whereHas would not work, how would you get about doing this?

UPDATE:

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"
    ]
}

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"
    ]
}
kratos
  • 2,465
  • 2
  • 27
  • 45
  • Similar question with regards to `belongsToMany` in the following link, and is answered: https://stackoverflow.com/questions/50741423/laravel-jensseger-mongodb-belongstomany-returns-empty-array/53446962#53446962 – Traxo Nov 23 '18 at 12:46

3 Answers3

6

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();
Sletheren
  • 2,435
  • 11
  • 25
  • But that doesn't work. That is it does get all the lawyers but the where clause has no effect. Even if i put category as 'ABC', it still gets all the lawyers. – kratos Sep 05 '17 at 15:47
  • weird! I tested it on my project with mangoDB and it works as expected, isn't something with your foreign keys? – Sletheren Sep 05 '17 at 16:37
  • I added the two mongo documents in my question above. Thanks a lot for helping me by the way. – kratos Sep 05 '17 at 16:47
  • Even if I do $lawyers = App\Lawyer::with(['cases'=> function($q){ $q->where('categorynnnnnnnnnnnnn', '=', 'DUI'); }])->get(); It still returns the result-set with all the lawyers – kratos Sep 05 '17 at 16:59
2
return $this->belongsToMany('App\LawCase');

put the foriegn key in your relationships

return $this->belongsToMany('App\LawCase', 'foreign_key');
  • This syntax is incorrect, see my answer (unless you were using older version when it was correct when you answered this?). – Traxo Nov 23 '18 at 12:52
0

Apparently conversion is not right (if it even exists?).
So set keys manually:

Case model:

public function lawyers()
{
    return $this->belongsToMany('App\Lawyer', null, 'case_ids', 'lawyer_ids');
}

Lawyer model:

public function cases()
{
    return $this->belongsToMany('App\LawCase', null, 'lawyer_ids', 'case_ids');
}

Note that we need two-way relation meaning layer_ids in Case model, and case_ids in Lawyer model (database entries).

Using attach should update both tables properly, for example:

$lawyer->cases()->attach($case->id)

(We need corresponding ids in both tables in order for relations to display properly)

Traxo
  • 18,464
  • 4
  • 75
  • 87