0

这个应用程序有一个错误模型和一个用户模型。用户可以“关注”Bug。这似乎是一个数据透视表。使用迁移和模型关系实现这一点的最简单方法是什么?我正在按照追随者表的思路思考:

    Schema::create('followers', function (Blueprint $table) {
        $table->increments('id');
        $table->uuid('bug_id');
        $table->uuid('user_id');
        $table->index(['bug_id', 'user_id']);
        $table->timestamps();
        $table->softDeletes();
    });

我最难过的是 Bugs 模型中的追随者关系。这是我目前所在的位置:

public function followers()
{
    return $this->belongsToMany(User::class, 'followers', 'bug_id', 'user_id');;
}

这看起来确实不太对劲。雄辩的数据透视表新手,非常感谢您的帮助!

4

1 回答 1

1

您正在寻找一个多对多关系,它需要一个中间表bug_follower,就像您创建一个中间表一样,您还需要像您一样创建一个 from Bugto关系,还需要创建一个 from toFollower关系。你在正确的轨道上。FollowerBugs

总的来说,您将有 2 个模型:BugFollower

您还将有 3 张桌子:bugs, followers,bug_follower

Bug以及Follower彼此之间的 2 种关系


编辑:这就是您的关系需要的样子:

public function followers()
{
    return $this->belongsToMany(User::class, 'followers', 'bug_id', 'user_id');
}

您可以在文档中查看有关主题的更多信息。

于 2019-01-24T18:59:12.893 回答