在预先加载中提供额外条件,包括比较两列,laravel
Provide extra condition in eager loading which includes comparing of two columns, laravel
我有 table 学生、个人资料、科目和数据中心 table profile_subject
-学生--------{id,profile_id,year}
-个人资料----------{id}
-profile_subject--{profile_id,subject_id,年份}
-科目----------{id}
我想 select 一个 ID 为 5 的学生,并且急切地加载学生年级的个人资料和科目。
像这样:
$student = Student::with('profile','profile.subjects')->find(5);
但是我还要插入条件
->wherePivot('year','=','students.year')
在某处。怎么做?
此查询不会完成工作,因为它将搜索 "students.year" 文学
年份的记录
使用lazy eager loading。此代码不会创建任何其他查询,它会创建与 with()
相同数量的查询:
$student = Student::find(5);
$sudent->load(['profile', 'profile.subjects' => function ($q) use ($student) {
$q->wherePivot('year', $student->year);
}]);
我有 table 学生、个人资料、科目和数据中心 table profile_subject
-学生--------{id,profile_id,year}
-个人资料----------{id}
-profile_subject--{profile_id,subject_id,年份}
-科目----------{id}
我想 select 一个 ID 为 5 的学生,并且急切地加载学生年级的个人资料和科目。
像这样:
$student = Student::with('profile','profile.subjects')->find(5);
但是我还要插入条件
->wherePivot('year','=','students.year')
在某处。怎么做? 此查询不会完成工作,因为它将搜索 "students.year" 文学
年份的记录使用lazy eager loading。此代码不会创建任何其他查询,它会创建与 with()
相同数量的查询:
$student = Student::find(5);
$sudent->load(['profile', 'profile.subjects' => function ($q) use ($student) {
$q->wherePivot('year', $student->year);
}]);