【发布时间】:2019-03-06 05:13:46
【问题描述】:
我有 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|
----------------------------------
这是模型:
学生.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();
}
但如果可能的话,我想使用关系而不是函数来获取教师数据
【问题讨论】: