我的模型中有两个函数:function getPersonsByGroup($groupId, $callback) { $group = StutGroup::where('stg_id', $groupId)->get(); $persons = []; foreach($group as $gr) { foreach($gr->students as $stud) { $persons[] = $stud->person; } } return $callback(collect($persons)); }function joinStudentsToPersons($person) { return $person->each(function ($pers) { $pers->student = \DB::connection('pgsql2')->table('students')->where('stud_pers_id', $pers->pers_id)->get(); }); }我试图调用getPersonsByGroup控制器中的函数,传递对回调的引用,如下所示:$students = $studGroup->getPersonsByGroup($request->group, $studGroup->joinStudentsToPersons);但是如果我将匿名函数传递给getPersonsByGroup一切正常:$students = $studGroup->getPersonsByGroup($request->group, function($person) { return $person->each(function ($pers) { $pers->student = \DB::connection('pgsql2')->table('students')->where('stud_pers_id', $pers->pers_id)->get(); });});我究竟做错了什么?
1 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
如果您想保留这种结构,问题的解决方案是使方法返回闭包,如下所示:
function joinStudentsToPersons() {
return function ($person) {
$person->each(function ($pers) {
$pers->student = \DB::connection('pgsql2')->table('students')
->where('stud_pers_id', $pers->pers_id)
->get();
});
};
}
然后这样称呼它:
$students = $studGroup->getPersonsByGroup($request->group, $studGroup->joinStudentsToPersons());
- 1 回答
- 0 关注
- 101 浏览
添加回答
举报
0/150
提交
取消