簡體   English   中英

Laravel遷移為關注者/關注者

[英]Laravel Migration for Followers / Following

我正在為用戶關注者設置遷移。 我想用它來檢查一個用戶是否在關注另一個用戶,以及是否允許他查看發布者的帖子並對其發表評論。

我根據此示例進行此操作:

例

我這樣寫:

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');
    }
}

該表的用法如下:

1)用戶可以(單擊)關注發布者,因此可以按照表成為關注者。 示例Publisher_id = 1; Follower_id = 2; enable_follow = 1; Publisher_id = 1; Follower_id = 2; enable_follow = 1; ID為2的用戶現在緊隨用戶ID為1之后,可以在Feed中看到該用戶的帖子,例如在twitter中。 當enable_follow設置為1時,使用戶能夠查看帖子並評論這些帖子

2)發布者可以禁止用戶查看或評論他的帖子,例如Publisher_id = 1; Follower_id = 2; enable_follow = 0; 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;

因此,基本上'follower_id'實際上既充當關注者又充當評論者。

我想說這個架構應該能夠處理您正在尋找的功能。

我唯一不了解的是可為空的外鍵。 我不確定是否有必要使它們為可空值,或者如果一個為空則意味着什么。 如果將它們設置為可空值,或者如果有可能,也可能不會損害它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM