jeudi 24 janvier 2019

Laravel 5.7 - Users/followers pivot table to create a follower (user) to Bug relationship

This app has a Bug model and a User model. Users can "follow" Bugs. This seems like a pivot table. What's the simplest way to implement this with a migration and Model relationship? I'm thinking along the lines of a bugs_followers table:

Schema::create('bugs_followers', function (Blueprint $table) {
        $table->increments('id');
        $table->uuid('bug_id');
        $table->uuid('user_id');
        $table->index(['bug_id', 'user_id']);
        $table->timestamps();
        $table->softDeletes();
    });

Where I'm mostly stumped is the followers relationship in the Bugs Model. This is where I'm currently at:

public function followers()
{
    return $this->belongsToMany('bugs_followers', 'ant_id', 'user_id')->withPivot();
}

This really doesn't seem right. New to eloquent pivot tables, so really appreciate the help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire