Laravel 带约束的多态嵌套式即时加载
我有以下课程:Laravel 带约束的多态嵌套式即时加载,laravel,Laravel,我有以下课程: students-----{id,profile_id,year} profiles-----{id} subjects-----{id}-is learnable groupsSbj-----{id}-is learnable learnables----{profile_id,learnable_id,learnable_type,year} groupssbj_subject--{subject_id,groupsbj_id} 我想加载一个id为5的学生,它是p
students-----{id,profile_id,year}
profiles-----{id}
subjects-----{id}-is learnable
groupsSbj-----{id}-is learnable
learnables----{profile_id,learnable_id,learnable_type,year}
groupssbj_subject--{subject_id,groupsbj_id}
我想加载一个id为5的学生,它是profile,以及该profile的learnables,其中learnables.year=student.year
我喜欢懒洋洋的
$student = Student::find(5);
$student->load(['profile.subjects'=>function($query) use($student){
$query->wherePivot('year','=',$student->year);
}],['profile.groupsSbj'=>function($query) use($student){
$query->wherePivot('year','=',$student->year);
}],'profile.groupsSbj.subjects');
如果我调用$student.profile.subjects,我会得到带有约束的主题,但组$student.profile.groupsSbj不受约束,如果我将其分为两个加载
$student = Student::find(5);
$student->load(['profile.subjects'=>function($query) use($student){
$query->wherePivot('year','=',$student->year);
}]);
$student->load(['profile.groupsSbj'=>function($query) use($student){
$query->wherePivot('year','=',$student->year);
}],'profile.groupsSbj.subjects');
然后,组被正确地约束,但受试者没有,这意味着他们全部被加载。我可能会通过再次编写查询来约束它们,但我只是好奇它为什么会这样?laravel relationship类是否有问题,或者我是否做错了什么,因为我以前从未使用过此多态类尝试将所有参数放在
加载([/*所有参数都在一个数组中*/])
@apokryfos工作@我不知道他们分开了,太好了,谢谢!尝试将所有参数放在加载中的单个数组中([/*所有参数都在一个数组中*/])
@apokryfos有效@我不知道他们分开了,太好了,谢谢!