Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

Laravel orm多对多关系如何获得中间表的附加字段的内容?

table:

users
    id
questions
    id
user_questions
    user_id
    question_id
    desc // 附加字段
    unique(user_id, question_id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

大致这个样子,定义模型关系的时候

return $this->belongsToMany('AppRole')->withPivot('column1', 'column2');

获取数据的时候

$user = AppUser::find(1);

foreach ($user->roles as $role) {
    echo $role->pivot->column1;
}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...