我尝试在注册管理器中循环使用foreach,如下所示
SELECT DISTINCT COALESCE(u.club, su.club) AS club
, COALESCE(count(u.club), count(su.club)) AS clubcount
FROM Users u
INNER JOIN Suggestedusers su ON su.club = u.club
GROUP BY u.club,su.club
ORDER BY clubcount DESC
OR
$subjectStartID = Input::get('substID');
$subjectStart = SubjectStart::find($subjectStartID);
//To get division_ids by the relationships using SubjectStartID
$divisionIDs =$subjectStart->teachersubject->subject->divisions ;
// loop over $divisionIDs to get div_ids
foreach ($divisionID as $div)
{
$div->id ;
}
return var_dump($div->id);
假设循环的结果只得到最后一个id,
例如
如果div_ids(1,2)的结果是(2)
我需要foreach让div_ids在以下的joinQuery中使用它
return var_dump([$div->id]);
有什么建议吗?
答案 0 :(得分:1)
我不能说laravel,但从php
的角度来看,你应该使用一个容器数组来存储foreach循环中返回的所有id。
imho,所以你的代码应该如下所示......
$subjectStartID = Input::get('substID');
$subjectStart = SubjectStart::find($subjectStartID);
//To get division_ids by the relationships using SubjectStartID
$divisionIDs =$subjectStart->teachersubject->subject->divisions ;
$studivs = array();
// loop over $divisionIDs to get div_ids
foreach ($divisionID as $div)
{
$studivs[] = $div->id ;
}
现在,变量$studivs
包含了您需要的所有ID。