【发布时间】:2020-01-15 06:03:14
【问题描述】:
我有一个 laravel 数据库设计,其中每个结果都与问题有 belongsToMany 关系,每个问题都与主题有 belogsTo 关系。我想用 laravel 上的单个语句从结果中获取主题名称
我已经从 laravel 文档中尝试过这个 https://laravel.com/docs/5.6/eloquent-relationships#has-many-through
这是我的结果模型代码
public function questions(){
return $this->belongsToMany('App\questions', 'results_questions', 'result_id', 'questions_id');
}
问题模型的代码
public function subject(){
return $this->belongsTo('App\Subject','subject_id');
}
这是我从文档中尝试过的(不起作用)
public function subjects(){
return $this->hasManyThrough(
'App\Subject',
'App\questions',
'subject_id', // Foreign key on question table...
'questions_id', // Foreign key on result table...
'id', // Local key on question table...
'id' // Local key on result table...
);
}**strong text**
【问题讨论】:
标签: laravel laravel-5 foreign-keys relational-database eloquent-relationship