我的模型中有两个函数:
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();
});
});
我究竟做错了什么?
临摹微笑