我想在laravel中建立三个表之间的关系。目前我有三个型号
教室,
public function subjects(){
return $this->belongstoMany('Subject','subject_section_classroom');
}
栏目下,
的主题
我的表格
classrooms(id, name)
sections(id, name)
subjects(id, name)
subject_section_classroom( id, classroom_id, section_id, subject_id)
在我的课堂控制器中我有
public function assignsubjects($class_id, $section_id){
$classroom = Classroom::find($class_id);
$section = Section::find($section_id);
$subjects = Subject::lists('name','id');
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1);
$subjects = Subject::lists('name','id');
return view('assignedit', compact('classroom','section','subjects', 'selected_subjects'));
}
但是我无法从上面的关系中获取selected_subjects。当我试图获取上述查询的sql(使用 - > toSQL())时,我得到了
`"select * from `myschool_subjects` inner join `myschool_subject_section_classroom` on `myschool_subjects`.`id` = `myschool_subject_section_classroom`.`subject_id` where `myschool_subject_section_classroom`.`classroom_id` = ? and `section_id` = ?"`
我无法想象我在这里做错了什么。 请帮助。
答案 0 :(得分:0)
我认为你的问题来自于
php
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1);
将其更改为:
php
$selected_subjects = $classroom->subjects()->where('section_id', '=', 1)->get();