search

Home  >  Q&A  >  body text

How to get the content of additional fields of the intermediate table in Laravel orm many-to-many relationship?

table:

users
    id
questions
    id
user_questions
    user_id
    question_id
    desc // 附加字段
    unique(user_id, question_id)
仅有的幸福仅有的幸福2863 days ago365

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 16:49:36

    It looks roughly like this, when defining model relationships

    return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
    

    When getting data

    $user = App\User::find(1);
    
    foreach ($user->roles as $role) {
        echo $role->pivot->column1;
    }
    
    
    

    reply
    0
  • Cancelreply