简体   繁体   中英

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. Example 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. The user is enabled to see the posts and to comment on those posts as enable_follow is set to 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; 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; Publisher_id = 1; Follower_id = 2; enable_follow = 0;

So basically 'follower_id' is really acting both as follower and commenter.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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