【发布时间】:2018-06-18 23:38:28
【问题描述】:
我在这三个模型之间有关系
class Teach extends Model {
public function departments() {
return $this->belongsToMany('App\Department', 'teach', 'id', 'department_id');
}
public function subjects() {
return $this->belongsToMany('App\Subject', 'teach', 'id', 'subject_id');
}
public function teachers() {
return $this->belongsToMany('App\Teacher', 'teach', 'id', 'teacher_id');
}
}
我想使用这些关系列出特定教师的所有部门和科目我在我看来已经尝试过,但它不起作用 $teacher->teach->部门;
【问题讨论】:
-
好吧,这是你的 Teach 模型,而不是 Teacher(你写了你想在视图中显示教师),你没有包含你的视图并且你没有包含你得到的错误
-
试试这个:
$teacher->teach()->departments