Open foron23 opened 2 years ago
Try this library: Laravel mongo auto sync. It handles relationship on embedded models
I tried installing the package, unfortunately, it seems it does not have support for Laravel 8
My result is empty too
Hi guys @foron23 @mongobird, we have just released a new version compatible with L8. Check it out version 2 and let me know.
Description:
My use case conforms the relationship between 3 entities/models:
The intention is to embed every Post with as many translations as needed, being these defined by the PostLanguage entity. The Content might be dependant on the language selected, therefore it needs to be related to the PostLanguage.
The Content is uploaded to the backend separately, and later related to the according posts, which means a Content document might be related to many PostLanguage, as well as a PostLanguage might have many Contents related to it. As a consequence, the relationship between both of them is belongsToMany for each of them.
Steps to reproduce
namespace Modules\Project\Entities;
use Jenssegers\Mongodb\Eloquent\Model;
class Post extends Model { protected $connection = 'mongodb';
}
namespace Modules\Project\Entities;
use Jenssegers\Mongodb\Eloquent\Model;
class PostLanguages extends Model { public $timestamps = false;
}
namespace Modules\Project\Entities;
use Jenssegers\Mongodb\Eloquent\Model;
class Content extends Model { public $timestamps = false;
}
$newPost = new Post([ "post_type" => 1, "location "=> [ "lat"=> 0, "lng"=> 0 ]; ])
$new_post_langs = new PostLanguages([ "name"=> "example name", "description"=> "example description", ]) $new_post_langs->save();
$content = Content::find($content_id); $content->posts()->attach($new_post_langs->_id);
$newPost->associate($new_post_langs); $newPost->save();
$content = Content::find("627a68ba1fb961387100c34f") $posts = $content->posts()
$post = Post::find("627a15b31fb961387100c345") $languages = $post->languages()
$post = Post::find("627a15b31fb961387100c345");
=> { "_id": ObjectId("627a15b31fb961387100c345") "post_type ": "1", "location ": { "lat": 0, "lng": 0 }, "created_at":ISODate("2016-01-03T21:21:38.032Z"), "updated_at":ISODate("2016-01-03T21:21:38.032Z"), "languages":[ { "_id" : ObjectId("627a15b31fb961387100c344") "name": "example name", "description": "example description", "contents" : ["625420319af6b94ab40e7087"] } ] }
$languages = $post->languages() => array( { "_id" : ObjectId("627a15b31fb961387100c344") "name": "example name", "description": "example description", "contents" : ["627a68ba1fb961387100c34f"]
} )
foreach($languages as $lang){ $contents = $lang->contents() } => array( { "_id" : ObjectId("627a68ba1fb961387100c34f") "url ": "https://www.example.com/", "type": 1, "posts": ["627a15b31fb961387100c344"] } )
$content = Content::find("627a68ba1fb961387100c34f")
=> { "_id" : ObjectId("627a68ba1fb961387100c34f") "url ": "https://www.example.com/", "type": "1", "posts": ["627a15b31fb961387100c344"] }
$posts = $content->posts()
=> array( )