I made a Laravel 5 application with MongoDB. For MongoDB, I'm using jenssegers/laravel-mongodb.
I have a MongoDB object like this:
{
"_id" : ObjectId("556c47677347b041b6004bfd"),
"name" : "Lucht?",
"type" : "onderzoeksvragen",
"levels" : [
"5",
"6"
],
"data" : {
"name" : "Lucht?",
"description" : "<p>
\tWat is lucht?</p>
<p>
\tHoe sterk is lucht?</p>
<p>
\tWat kun je met lucht?</p>
",
"coach_information" : null
},
"updated_at" : ISODate("2015-06-01T11:52:23.399Z"),
"created_at" : ISODate("2015-06-01T11:52:07.536Z"),
"related_ids" : [
"556c47627347b041b6004263"
]
}
The field related_ids
contains an array with ObjectId's who are related to this object. I want to get this object with the related objects. The related objects are the same type as the main object. So, they are in the same collection.
I have read the documentation on Github (https://github.com/jenssegers/laravel-mongodb) and found this:
The belongsToMany relation will not use a pivot "table", but will push id's to a
related_ids
attribute instead.use Jenssegers\Mongodb\Model as Eloquent; class User extends Eloquent { public function groups() { return $this->belongsToMany('Group', null, 'users', 'groups'); } }
But, it doesn't work for me. My code:
Controller ExploreController.php
$explore = Explore::with('related')->find('556c47677347b041b6004bfd');
Model Explore.php
public function related()
{
$this->belongsToMany('App\Explore', null, 'explore' 'explore');
}
The result of this includes only the main object, not the related objects.