我有3个表faculties
,students
,program_studies
。我正在使用Lumen 5.7
这是表格:
学院表:
-----------------
|id|faculty_name|
-----------------
| 1| test |
-----------------
学生桌:
----------------------------------
|id|student_name|program_study_id|
----------------------------------
| 1| student 1 | 1|
----------------------------------
| 2| student 2 | 1|
----------------------------------
program_studies表:
----------------------------------
|id|program_study_name|faculty_id|
----------------------------------
| 1| program study | 1|
----------------------------------
这是模型:
Student.php
public function programStudy()
{
return $this->belongsTo(ProgramStudy::class);
}
PorgramStudy.php
public function faculty()
{
return $this->belongsTo(Faculty::class);
}
Faculty.php
nothing relation in faculty model
如何在学生模型中获取具有关联关系的教师数据?
现在,我在Student.php中创建如下函数:
public function getFaculty()
{
return $this->programStudy()->first()->faculty()->first();
}
但如果可能的话,我想使用关系非函数获取教职数据
答案 0 :(得分:0)
您可以使用hasOneThrough
关系将您的学生链接到学院。
public function faculty()
{
return $this->hasOneThrough(Faculty::class, ProgramStudy::class);
}