mercredi 31 mai 2017

Laravel user can follow Comment, Category, Post etc

I wanted to implement a follow system where a User can follow Comment, Category, Post and more. I have tried using Laravel Polymorphic relations for this but could not wrap my head around it. If someone can guide me it will be great.

Here is what I have tried.

User Model

public function categories()
{
    return $this->morphedByMany(Category::class, 'followable', 'follows')->withTimestamps();
}

Category Model

public function followers()
{
    return $this->morphMany(Follow::class, 'followable');
}

Follow Model

public function followable()
{
    return $this->morphTo();
}

public function user()
{
    return $this->belongsTo(User::class);
}

Follow migration

Schema::create('follows', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->morphs('followable');
    $table->timestamps();
});

How can I get the all the categories, comments followed by a user. Also how I can get the Followers of a Cateogry or Commnets etc.

Please help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire