首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Mongo enbedsMany不嵌入文档

Laravel Mongo enbedsMany不嵌入文档
EN

Stack Overflow用户
提问于 2016-10-13 23:01:27
回答 1查看 358关注 0票数 0

使用经典的帖子和评论示例,我发现embedsMany关系似乎不能正常工作,而hasMany工作得很好。也就是说,在hasMany中,我看到了外键等等,但是在embedsMany中,我在Posts DB中看不到任何嵌入的文档。

代码语言:javascript
复制
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');
    }


}

我要保存的代码:

代码语言:javascript
复制
 $comment = new Comment();
  $comment->text = 'This is a comment';
  $comment->save();
  $post = new Post();
  $post->text='some text blah blah';
  $post->comments()->save($comment);
EN

回答 1

Stack Overflow用户

发布于 2016-10-14 05:39:51

我想通了。我不应该保存评论文档。

代码语言:javascript
复制
 $comment = new Comment();
  $comment->text = 'This is a comment';
//  $comment->save();
  $post = new Post();
  $post->text='some text blah blah';
  $post->comments()->save($comment);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40024570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档