Additional pivot column storing/retrieving

Posted 3 years ago by kickthemooon

The models are:

Campaign
User

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:

campaign_id
user_id
comment

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) {
            $table->integer('campaign_id');
            $table->integer('user_id');
            $table->text('comment');
        });

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