使用经典的帖子和评论示例,我发现embedsMany关系似乎不能正常工作,而hasMany工作得很好。也就是说,在hasMany中,我看到了外键等等,但是在embedsMany中,我在Posts DB中看不到任何嵌入的文档。
class Post extends Moloquent
{
use SoftDeletes;
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection = 'mongodb';
public function comments()
{
return $this->embedsMany('App\Comment');
}
}
class Comment extends Moloquent
{
use SoftDeletes;
/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection = 'mongodb';
/**
* Get the case that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post');
}
}我要保存的代码:
$comment = new Comment();
$comment->text = 'This is a comment';
$comment->save();
$post = new Post();
$post->text='some text blah blah';
$post->comments()->save($comment);发布于 2016-10-14 05:39:51
我想通了。我不应该保存评论文档。
$comment = new Comment();
$comment->text = 'This is a comment';
// $comment->save();
$post = new Post();
$post->text='some text blah blah';
$post->comments()->save($comment);https://stackoverflow.com/questions/40024570
复制相似问题