在laravel控制器中的foreach

时间:2015-05-03 08:23:05

标签: php laravel laravel-4 laravel-5

我尝试在注册管理器中循环使用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]);

有什么建议吗?

1 个答案:

答案 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。