简体   繁体   English

Laravel迁移为关注者/关注者

[英]Laravel Migration for Followers / Following

I am setting up a migration for user followers. 我正在为用户关注者设置迁移。 I want to use this to check if a user is following another user and if he is enabled to see and comment on the publisher posts. 我想用它来检查一个用户是否在关注另一个用户,以及是否允许他查看发布者的帖子并对其发表评论。

I am doing this based on this example: 我根据此示例进行此操作:

例

I wrote this: 我这样写:

class CreateFollowersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('followers', function (Blueprint $table) {

            $table->unsignedInteger('publisher_id')->nullable()->unsigned();
            $table->unsignedInteger('follower_id')->nullable()->unsigned();
            $table->boolean('enable_follow')->default('1')->unsigned();
            $table->timestamps();

            $table->foreign('publisher_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->foreign('follower_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        schema::drop('followers');
    }
}

Use of this table should be as follow: 该表的用法如下:

1) a User can (click) follow a Publisher and so become a follower as per the table. 1)用户可以(单击)关注发布者,因此可以按照表成为关注者。 Example Publisher_id = 1; Follower_id = 2; enable_follow = 1; 示例Publisher_id = 1; Follower_id = 2; enable_follow = 1; Publisher_id = 1; Follower_id = 2; enable_follow = 1; The user with id 2 is now following user id 1 and can see the posts of that user on a feed like for example in twitter. ID为2的用户现在紧随用户ID为1之后,可以在Feed中看到该用户的帖子,例如在twitter中。 The user is enabled to see the posts and to comment on those posts as enable_follow is set to 1 当enable_follow设置为1时,使用户能够查看帖子并评论这些帖子

2) A Publisher can ban a user from seeing or commenting on his posts, example Publisher_id = 1; Follower_id = 2; enable_follow = 0; 2)发布者可以禁止用户查看或评论他的帖子,例如Publisher_id = 1; Follower_id = 2; enable_follow = 0; Publisher_id = 1; Follower_id = 2; enable_follow = 0;

Question: which users can see ( after a search for posts, not on the feed ) and comment on other users posts? 问题:哪些用户可以看到( 在搜索帖子之后,不在提要中 )并评论其他用户的帖子? Answer: every user unless the publisher, after getting an unwanted comment, decides to ban the user who posted the unwanted comment. 答:每个用户,除非发布者在收到不需要的评论后决定禁止发布不需要的评论的用户。

Before commenting there is no relationship at all in the followers table between the two users. 在发表评论之前,两个用户之间的关注者表中根本没有任何关系。 The relationship is created after the publisher bans the user and will be like in example number 2 Publisher_id = 1; Follower_id = 2; enable_follow = 0; 该关系是在发布者禁止用户之后创建的,并且类似于示例2中的Publisher_id = 1; Follower_id = 2; enable_follow = 0; Publisher_id = 1; Follower_id = 2; enable_follow = 0;

So basically 'follower_id' is really acting both as follower and commenter. 因此,基本上'follower_id'实际上既充当关注者又充当评论者。

I would say this schema should be able to handle the functionality you are looking for. 我想说这个架构应该能够处理您正在寻找的功能。

The only thing I don't understand is the nullable foreign keys. 我唯一不了解的是可为空的外键。 I'm not sure it's necessary to have them nullable or what it would mean if one was null. 我不确定是否有必要使它们为可空值,或者如果一个为空则意味着什么。 It also probably would not hurt having them nullable or if it would ever be possible. 如果将它们设置为可空值,或者如果有可能,也可能不会损害它们。

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

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