【发布时间】:2019-12-09 12:59:42
【问题描述】:
更新:
我的结果差不多了,我只想过滤我正在寻找的“当前”学生
我更新的模型:
// Student.php
public function enrolled()
{
return $this->belongsToMany('App\Classroom', 'inscribed_students')
->with('enrolled_steps.student_process')
->whereRaw('start_at >= curdate()');
}
// Classroom.php
public function enrolled_steps()
{
return $this->hasMany('App\InscribedStudent');
}
// InscribedStudent.php
public function student_process()
{
return $this->hasMany('App\StudentSelectionProcess');
}
我的输出 json:
{
"id": 1,
"name": "This is a Student",
"email": "dborer@example.org",
"enrolled": [
{
"id": 31,
"name": "This is a Classroom",
"shift": "Morning",
"enrolled_steps: [
{
"id": 1,
"student_id": 1,
"classroom_id": 1,
"student_process": [
{
"id": 1,
"status": "Approved"
}
]
},
{
"id": 2,
"student_id": 2,
"classroom_id": 1,
"student_process": [
{
"id": 2,
"status": "Approved"
}
]
},
]
}
]
}
当前的问题是 enrolled_steps 是返回一个数组,但我过滤了一个学生,我该如何解决它以仅获取我当前的学生?
我的预期输出:
{
"id": 1,
"name": "This is a Student",
"email": "dborer@example.org",
"enrolled": [
{
"id": 31,
"name": "This is a Classroom",
"shift": "Morning",
"enrolled_steps: {
"id": 1,
"student_id": 1,
"classroom_id": 1,
"student_process": [
{
"id": 1,
"status": "Approved"
}
]
}
}
]
}
问题:
我的问题是我必须建立多重/硬性关系才能显示有关学生的信息。
+--------------------+
| Student |
+--------------------+
| id |
| name |
| email |
+--------------------+
+--------------------+
| Classroom |
+--------------------+
| id |
| name |
| shift |
+--------------------+
+--------------------+
| InscribedStudent |
+--------------------+
| id |
| student_id | << Foreign key
| classroom_id | << Foreign key
+--------------------+
+--------------------+
| SelectionProcess |
+--------------------+
| id |
| classroom_id | << Foreign key
| enabled |
+--------------------+
+-------------------------+
| StudentSelectionProcess |
+-------------------------+
| id |
| inscribed_student_id | << Foreign key
| selection_process_id | << Foreign key
| status |
+-------------------------+
我的查询生成器
$student = DB::table('students')
->join('inscribed_students', 'inscribed_students.student_id', '=', 'students.id')
->join('classrooms', 'classrooms.id', '=', 'inscribed_students.classroom_id')
->join('selection_processes', 'selection_processes.classroom_id', '=', 'classrooms.id')
// If exists show, else null
->leftjoin('student_selection_processes', 'student_selection_processes.selection_process_id', '=', 'selection_processes.id')
->select('students.*', 'classrooms.*', 'student_selection_processes.*')
->where([
['selection_processes.enabled', 1], // Very important
['students.id', $id]
])
->first();
但我认为这种方式非常混乱,仍然需要重新排列资源,所以我想知道是否可以将这个 Query 转换为 Eloquent 关系。。 p>
我预期的雄辩的 json 结果
{
"id": 1,
"name": "This is a Student",
"email": "dborer@example.org",
"enrolled": [
{
"id": 31,
"name": "This is a Classroom",
"shift": "Morning",
"process: {
"id": 5,
"status": "Approved"
}
}
]
}
我可以去教室,但我不知道如何获得流程
// StudentController.php - Student controller
$student = Student::with(['enrolled'])
->find($id);
// Student.php - Student Model
public function enrolled()
{
return $this->belongsToMany('App\Classroom', 'inscribed_students');
}
如果可能,如果 student_selection_processes 为 null,则不显示相关教室
【问题讨论】:
-
你能展示你的模型和数据库设计吗?
-
@DilipHirapara 我更新了模型结构
标签: laravel eloquent relationship laravel-query-builder