0
我想實現一個跟隨系統,其中User
可以遵循Comment
,Category
,Post
和更多。我曾嘗試使用Laravel Polymorphic關係爲此,但不能包裹我的頭。如果有人能指導我,那將會很棒。Laravel用戶可以按照評論,類別,發佈等
這是我試過的。
用戶模型
public function categories()
{
return $this->morphedByMany(Category::class, 'followable', 'follows')->withTimestamps();
}
分類模式
public function followers()
{
return $this->morphMany(Follow::class, 'followable');
}
後續型號
public function followable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
按遷移
Schema::create('follows', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->morphs('followable');
$table->timestamps();
});
我怎樣才能獲得所有categories
,comments
其次是用戶。我怎麼能得到一個Cateogry或Commnets等追隨者
請幫助。
還有一兩件事,我可以通過'$ user-> followers'獲得用戶關注者關於用戶關注的內容,如何讓所有用戶關注用戶。 – Saqueib