Laravel使用同一张表定义多对多关系 [英] Laravel defining a many-to-many relationship with the same table

查看:32
本文介绍了Laravel使用同一张表定义多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个 posts 表以及相应的 Post 模型.我希望每个帖子都有相关的帖子.由于帖子可以包含许多其他相关帖子,因此 posts 表和 posts 表(同一表)之间是多对多的关系.

因此,我创建了一个 related_posts 数据透视表及其相应的模型 RelatedPost .我想在两个模型中定义这种关系.像这样:

发布模型:

  public function related(){返回$ this-> belongsToMany(RelatedPost :: class,'related_posts','related_id','post_id');} 

RelatedPost 模型:

 公共功能posts(){返回$ this-> belongsToMany(Post :: class,'related_posts','post_id','related_id');} 

现在,在选择特定帖子后,在我的帖子控制器中,我想获取所有与其相关的帖子.所以我这样做:

  $ post-> related()-> get(); 

但是当我这样做时,我收到以下错误消息:

"SQLSTATE [42000]:语法错误或访问冲突:1066不是唯一的表/别名:'related_posts'(SQL:选择 related_posts .*, related_posts . related_id 作为 pivot_related_id related_posts . post_id 作为 pivot_post_id 来自 related_posts 内部联接 related_posts 上的 related_posts . id = related_posts . post_id 其中 related_posts . related_id = 1)"

这是我对数据透视表的迁移:

  Schema :: create('related_posts',function(Blueprint $ table){$ table-> increments('id');$ table-> unsignedInteger('post_id');$ table-> unsignedInteger('related_id');$ table-> timestamps();$ table-> foreign('post_id')->引用('id')-> on('posts')-> onDelete('cascade');$ table-> foreign('related_id')-> references('id')->('posts')-> onDelete('cascade');}); 

我到处搜索,尽管找到的解决方案确实很有意义,但我无法让其中任何一个发挥作用.

任何帮助将不胜感激!

解决方案

感谢@ d3jn对我的问题的评论,我得以解决我的问题.因此,我将解决方案发布在这里,以防万一其他人可能需要它.

我将 Post 模型与其自身相关联,而不是与枢轴模型 RelatedPost 相关.因此,我不需要 RelatedPost 模型.我只需要一个数据透视表( related_post ),关系的 id related_id post_id .

因此,在保持迁移不变的情况下,我只需要取消 RelatedPost 模型并在 Post 中更改我的 related()方法模型看起来像这样:

  public function related(){返回$ this-> belongsToMany(Post :: class,'related_posts','post_id','related_id');} 

现在一切正常.

So I have a posts table with a corresponding Post model. I want to have related posts for every post. Since a post can have many other related posts, it is a many-to-many relationship between the posts table and the posts table (same table).

So I created a related_posts pivot table with its corresponding model RelatedPost. I want to define this relationship in the two models. Like so:

Post model:

public function related()
{
 return $this->belongsToMany(RelatedPost::class, 'related_posts', 'related_id', 'post_id');
}

RelatedPost model:

public function posts()
{
  return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}

Now in my post controller after selecting a particular post, I want to get all its related posts. So I do this:

$post->related()->get();

But when I do this I get the following error message:

"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'related_posts' (SQL: select related_posts.*, related_posts.related_id as pivot_related_id, related_posts.post_id as pivot_post_id from related_posts inner join related_posts on related_posts.id = related_posts.post_id where related_posts.related_id = 1) "

This is my migration for the pivot table:

  Schema::create('related_posts', function (Blueprint $table) {
      $table->increments('id');
      $table->unsignedInteger('post_id');
      $table->unsignedInteger('related_id');
      $table->timestamps();

      $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
      $table->foreign('related_id')->references('id')->('posts')->onDelete('cascade');
  });

I have searched all over everywhere and though the solutions I've found really make sense I haven't been able to get any of them to work.

Any help will be very much appreciated!

解决方案

Thanks to @d3jn's comment on my question I was able to solve my problem. So I am posting the solution here just in case someone else might need it.

I am relating the Post model to itself not to the pivot model RelatedPost. So I don't need a RelatedPost model. I only need a pivot table (related_post), and the relation's ids namely related_id and post_id.

So with my migration unchanged, I only need to do away with the RelatedPost model and change my related() method in the Post model to look like this:

public function related()
{
  return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}

And now everything works.

这篇关于Laravel使用同一张表定义多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆