简体   繁体   中英

Retrieving data from pivot table column in manytomany relationship

users table has:

id   name   email

questions table has:

id   question 

question_user (pivot table) has an extra field user_question_rating.

user_id   question_id   user_question_rating

User and Question has Many relations.

User Model:

public function questions()
{
    return $this->belongsToMany('App\Question');
}

Question Model:

public function users()
{
    return $this->belongsToMany('App\User');
}

How do i find the user and question with given user_question_rating (eg 8)?

You can use withPivot() function:

    public function users()
    {
       return $this->belongsToMany('App\User')->withPivot('user_question_rating');
    }

otherwise by default only relation keys will be returned

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM