Laravel Mongo禁止许多人没有嵌入文件

时间:2016-10-13 15:01:27

标签: php mongodb laravel

使用经典的帖子和评论示例我看到embedsMany关系似乎没有工作,因为hasMany工作得很好。这是hasMany我看到外键和所有但嵌入很多我看不到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);

1 个答案:

答案 0 :(得分:0)

想出来了。我没有保存评论文件。

 $comment = new Comment();
  $comment->text = 'This is a comment';
//  $comment->save();
  $post = new Post();
  $post->text='some text blah blah';
  $post->comments()->save($comment);