简体   繁体   English

Laravel eloquent - 渴望加载嵌套关系

[英]Laravel eloquent - Eager loading nested relations

I have the following 3 tables:我有以下 3 个表:

  • Posts帖子
  • Comments注释
  • Tags标签

Posts:帖子:

    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'
    }

Comments:注释:

    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]
    }

Tags:标签:

    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 table has many comments and comments table has many tags associated with it.帖子表有很多评论,评论表有很多与之相关的标签。

How do i get all this tables together using eager loading?如何使用预先加载将所有这些表放在一起?

Something like :就像是 :

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

but this query always returning empty response in the tags relationship.但是这个查询总是在标签关系中返回空响应。

What am i doing wrong?我究竟做错了什么?

The desired result is something like this:想要的结果是这样的:


{
   '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'}
                           ],
              }
             ]
}

You can see that post contains comments inside it and comments contains tags relationship inside it as well.你可以看到帖子里面包含评论,评论里面也包含标签关系。

PS I am using the 'jenssegers/laravel-mongodb' package in my project and i am trying to do it without any raw expression. PS 我在我的项目中使用了 'jenssegers/laravel-mongodb' 包,我试图在没有任何原始表达式的情况下进行。

Thanks.谢谢。

You have defined your relationships wrong.你错误地定义了你的关系。 You have tags_ids as array in your comment model but instead you need many-to-many relationships for your tags.您的评论模型中有tags_ids作为数组,但您的标签需要多对多关系。 To implement this you have to define a new table comment-tag :要实现这一点,您必须定义一个新表comment-tag

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

Then in you Comment model modify tags relationship like this:然后在您的Comment模型中修改tags关系,如下所示:

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

The inverse of this relationship is:这种关系的逆是:

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

Then to eager load nested relationships, you may use "dot" syntax:然后急切加载嵌套关系,您可以使用“点”语法:

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

See Many-to-Many relationship on Laravel docs for more info.有关更多信息,请参阅 Laravel 文档上的多对多关系

you can use您可以使用

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

it will load all the comments as well as comments.tags它将加载所有评论以及comments.tags

ref link https://laravel.com/docs/6.x/eloquent-relationships in link search Nested Eager Loading链接搜索中的参考链接https://laravel.com/docs/6.x/eloquent-relationships Nested Eager Loading

UPDATE: You must fix your db schema更新:您必须修复您的数据库架构

The table structure goes like this:表结构是这样的:

- 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);
//...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM