Laravel雄辩-渴望加载嵌套关系

时间:2020-01-24 12:34:02

标签: php laravel mongodb eloquent eager-loading

我有以下3张桌子:

  • 帖子
  • 评论
  • 标签

帖子:

    class Post extends Eloquent
    {

        public function comments()
        {
            return $this->hasMany(Comment::class,'post_id','id');
        }

    }

    ** Post data **
    {
       'id' : 1,
       'title' : 'bla bla',
       'created_at: 'some date'
    }

评论:

    class Comment extends Eloquent
    {

        public function comments()
        {
            return $this->belongsTo(Post::class,'id');
        }

        public function tags()
        {
            return $this->hasMany(Tag::class,'id','tags_ids');
        }

    }

** Comments data **

    {
      'id' : 322,
      'active' : true
      'post_id' : 1,
      'created_at: 'some date',
      'tags_ids' : [1,2,3]
    }

标签:

    class Tag extends Eloquent
    {

        public function tags()
        {
            return $this->belongsTo(Comment::class,'tags_ids');
        }

    }

    ** Tags data **
    {
      {'id' : 1,
      'description' : 'some description1'
      },
      {'id' : 2,
      'description' : 'some description2'
      },
      {'id' : 3,
      'description' : 'some description3'
      }
    }

发布表有很多评论,评论表有很多与之相关的标签。

如何使用紧急加载将所有这些表放在一起?

类似的东西:

$post = Post::where('id',1)->with(['comments' => function($q) {
   $q->with('tags');
}])->first();

但是此查询始终在标签关系中返回空响应。

我在做什么错了?

所需的结果是这样的:


{
   'id' : 1,
   'title' : 'bla bla',
   'created_at: 'some date',
   'comments':[{
                  'id' : 322,
                  'active' : true
                  'post_id' : 1,
                  'created_at: 'some date',
                  'tags_ids' : [1,2,3],
                  'tags' : [
                            {'id' : 1,'description' : 'some description1'},
                            {'id' : 2,  'description' : 'some description2'},
                            {'id' : 3,'description' : 'some description3'}
                           ],
              }
             ]
}

您可以看到帖子中包含评论,评论中也包含标签关系。

P.S。我在我的项目中使用了“ jenssegers / laravel-mongodb”包,但我尝试不使用任何原始表达式。

谢谢。

3 个答案:

答案 0 :(得分:2)

您定义的人际关系错误。您的注释模型中有tags_ids作为数组,但是您的标签需要多对多关系。为此,您必须定义一个新表comment-tag

comment-tag
    comment_id - unsined big integer
    tag_id - unsigned big integer

然后在您的Comment模型中像这样修改tags关系:

class Comment extends Model
{
    /**
     * The tags that belong to the comment.
     */
    public function tags()
    {
        return $this->belongsToMany('App\Tag');
    }
}

这种关系的逆是:

class Tag extends Model
{
    /**
     * The comments that belong to the tag.
     */
    public function comments()
    {
        return $this->belongsToMany('App\Comment');
    }
}

然后急于加载嵌套关系,可以使用“点”语法:

$post = Post::with(['comments', 'comments.tags'])->find(1);

有关更多信息,请参见Laravel文档上的Many-to-Many relationship

答案 1 :(得分:1)

您可以使用

$post = Post::where('id',1)->with(['comments.tags'])->first();

它将加载所有评论以及comments.tags

参考链接https://laravel.com/docs/6.x/eloquent-relationships 在链接搜索Nested Eager Loading

答案 2 :(得分:0)

更新:您必须修复数据库架构

表结构如下:

- posts
    - id
    - title
    - ...

- comments
    - id
    - active
    - post_id 
    - created_at

- tags
    - id
    - description
    - comment_id 
    - created_at

// App\Post 
class Post extends Eloquent 
{

    public function comments()
    {
        return $this->hasMany(Comment::class,'post_id','id');
    }

}

// App\Comment 
class Comment extends Eloquent 
{

    public function post()
    {
        return $this->belongsTo(Post::class);
    }

    public function tags()
    {
        return $this->hasMany(Tag::class);
    }

}


//App\Tag    
class Tag extends Eloquent 
{

    public function comment()
    {
        return $this->belongsTo(Comment::class);
    }

}




//...
$post = Post::with(['comments', 'comments.tags'])->find(1);
//...