3 years ago

Additional pivot column storing/retrieving

Posted 3 years ago by kickthemooon

The models are:


User can publish campaigns so the user can be a publisher
but the user can also be a voter to a campaign.

So I need a many to many relationship for the Campaign model and User model and I need an additional field which will be a comment that a user can leave with the vote:


So I am not sure how I would handle storing the additional comment field inside the pivot table?
I added this relationship to the user model:

public function votes(){
        return $this->belongsToMany('App\Campaign')->withPivot('comment');

and this to the campaign model:

public function supporter(){
        return $this->hasMany('App\User');

I hope that is ok?

This is my migration:

Schema::create('campaign_user', function (Blueprint $table) {

Please sign in or create an account to participate in this conversation.