Laravel 用相同的 table 定义多对多关系
Laravel defining a many-to-many relationship with the same table
所以我有一个 posts
table 和相应的 Post
模型。我希望每个 post 都有相关的 post。由于一个 post 可以有许多其他相关的 post,因此 posts
table 和 posts
[=65 之间是多对多关系=](同table)。
所以我创建了一个 related_posts
枢轴 table 及其对应的模型 RelatedPost
。我想在两个模型中定义这种关系。像这样:
Post 型号:
public function related()
{
return $this->belongsToMany(RelatedPost::class, 'related_posts', 'related_id', 'post_id');
}
相关Post型号:
public function posts()
{
return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}
现在在我的 post 控制器中选择一个特定的 post 后,我想获得所有相关的 post。所以我这样做:
$post->related()->get();
但是当我这样做时,我收到以下错误消息:
"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) "
这是我对枢轴的迁移 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');
});
我到处搜索,虽然我找到的解决方案确实有意义,但我无法让它们中的任何一个工作。
非常感谢任何帮助!
感谢@d3jn 对我的问题的评论,我得以解决我的问题。所以我在这里发布解决方案以防其他人可能需要它。
我将 Post
模型与其自身相关联,而不是与枢轴模型 RelatedPost
相关联。所以我不需要 RelatedPost
模型。我只需要一个枢轴 table (related_post
),关系的 id
即 related_id
和 post_id
.
所以在我的迁移不变的情况下,我只需要取消 RelatedPost
模型并将 Post
模型中的 related()
方法更改为如下所示:
public function related()
{
return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}
现在一切正常。
所以我有一个 posts
table 和相应的 Post
模型。我希望每个 post 都有相关的 post。由于一个 post 可以有许多其他相关的 post,因此 posts
table 和 posts
[=65 之间是多对多关系=](同table)。
所以我创建了一个 related_posts
枢轴 table 及其对应的模型 RelatedPost
。我想在两个模型中定义这种关系。像这样:
Post 型号:
public function related()
{
return $this->belongsToMany(RelatedPost::class, 'related_posts', 'related_id', 'post_id');
}
相关Post型号:
public function posts()
{
return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}
现在在我的 post 控制器中选择一个特定的 post 后,我想获得所有相关的 post。所以我这样做:
$post->related()->get();
但是当我这样做时,我收到以下错误消息:
"SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'related_posts' (SQL: select
related_posts
.*,related_posts
.related_id
aspivot_related_id
,related_posts
.post_id
aspivot_post_id
fromrelated_posts
inner joinrelated_posts
onrelated_posts
.id
=related_posts
.post_id
whererelated_posts
.related_id
= 1) "
这是我对枢轴的迁移 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');
});
我到处搜索,虽然我找到的解决方案确实有意义,但我无法让它们中的任何一个工作。
非常感谢任何帮助!
感谢@d3jn 对我的问题的评论,我得以解决我的问题。所以我在这里发布解决方案以防其他人可能需要它。
我将 Post
模型与其自身相关联,而不是与枢轴模型 RelatedPost
相关联。所以我不需要 RelatedPost
模型。我只需要一个枢轴 table (related_post
),关系的 id
即 related_id
和 post_id
.
所以在我的迁移不变的情况下,我只需要取消 RelatedPost
模型并将 Post
模型中的 related()
方法更改为如下所示:
public function related()
{
return $this->belongsToMany(Post::class, 'related_posts', 'post_id', 'related_id');
}
现在一切正常。