This is a Laravel / Mongo DB questions. I've got two models that are on mongo db.
Model 1 > Task
{
"user_ids": [
"id_1",
"id_2",
]
}
Then my model two
Model 2 > User
{
"_id": "id"
}
Now from my Model 1 (Task), I want to create a belongsToMany relationship so I can use the protected $with to fetch the objects in one go.
I have this in my Model 1 class
class Task extends Model
{
protected $with = [
'users'
];
public function users()
{
return $this->belongsTo(
User::class,
null,
'_id',
'user_ids'
)
}
}
Now is the interesting bit. So it returns the object and an empty array for users. What am I doing wrong?
Thanks in advance
Okay did a lot of look around ad dived into the vendor folders to figure out you cant actually do it.
The thing is with the references it ends up sending a multi-dimensional array which ends up breaking the whereIn function of eloquent.
This may be an area that the relation builder to improve.
What I did was to let it stay as an array and use a second function to query the results based on the ids.