laravel foreach循环中的多个变量

时间:2018-07-01 21:48:58

标签: php laravel foreach eloquent

我试图在视图中包含两个变量,这些变量在一个foreach循环中循环通过一个数组,以显示该年给定团队的获胜和失败记录。如果可以将相同的空数组用于两者,那么很酷,但这不是我要在这里做的...

public function show($id)
    {
       $seasons =( [ 2005, 2006,2007,
            2008,
            2009,
            2010,
            2011,
            2012,
            2013,
            2014,
           2015,
           2016]);

          $seasonFinalWins=[];
          $seasonFinalLosses=[];


        foreach($seasons as $homeFinalWin) {
            $seasonFinalWins[$homeFinalWin]=GameData::where('team', $id)->
            where('win', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $homeFinalWin)
            ->count();
           }

           foreach($seasons as $homeFinalLoss) {
            $seasonFinalLosses[$homeFinalLoss]=GameData::where('team', $id)->
            where('loss', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $homeFinalLoss)
            ->count();
           }

查看

 @if($seasonFinalWins)
 @foreach($seasonFinalWins as $seasonFinalWin)
  <td>{{$seasonFinalWin}}</td>
 @endforeach
 @endif

如何在上述视图的foreach中包括$ seasonFinalLosses?

我已经尝试过结合...

 $finalRecords=([$seasonFinalLosses, $seasonFinalWins]);

并查看...

 @if($finalRecords)
 @foreach($finalRecords as $finalRecord)
  <td>{{$finalRecord->$seasonFinalWins}}</td>
 @endforeach
 @endif

但是我得到一个数组到String错误。我需要知道是否有一种方法可以使两个变量可以通过->或任何其他方式在相同的foreach上调用?

需要明确的是,我每年都会显示“获胜次数”列,但是我需要一种将损失包括在同一列中的方法。谢谢。

3 个答案:

答案 0 :(得分:0)

使用array_merge

$finalRecords = array_merge($seasonFinalLosses, $seasonFinalWins);

答案 1 :(得分:0)

您不能使用此代码。希望对您有用:

getMessagesFor方法:

public function getMessagesFor(Request $request)
{

    $seasons = [ 2005, 2006,2007,
        2008,
        2009,
        2010,
        2011,
        2012,
        2013,
        2014,
        2015,
        2016];

    $seasonFinal=[];
    foreach($seasons as $session) {
        $seasonFinal['win'][$session]=GameData::where('team', $id)->where('win', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $session)
            ->count();
        $seasonFinal['loss'][$session]=GameData::where('team', $id)->
        where('loss', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $session)
            ->count();
    }


return view('welcome3',compact('seasonFinal'));

}

welcome3视图:

<table border="1">

@if($seasonFinal)
    @foreach($seasonFinal as $key=>$years)
        <td>type : {{$key}}</td>
        <td>count </td>
        @foreach($years as $year=>$count)
            <tr>
                <td>{{$year}}</td>
                <td>{{$count}}</td>
            </tr>
        @endforeach
    @endforeach
@endif

</table>

结果:

enter image description here

<table border="1">

                        <tbody><tr><td>type : win</td>
            <td>count </td>
                            </tr><tr>
                    <td>2005</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2006</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2007</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2008</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2009</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2010</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2011</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2012</td>
                    <td>1</td>
                </tr>
                            <tr>
                    <td>2013</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2014</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2015</td>
                    <td>2</td>
                </tr>
                            <tr>
                    <td>2016</td>
                    <td>1</td>
                </tr>
                                <tr><td>type : loss</td>
            <td>count </td>
                            </tr><tr>
                    <td>2005</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2006</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2007</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2008</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2009</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2010</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2011</td>
                    <td>1</td>
                </tr>
                            <tr>
                    <td>2012</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2013</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2014</td>
                    <td>1</td>
                </tr>
                            <tr>
                    <td>2015</td>
                    <td>0</td>
                </tr>
                            <tr>
                    <td>2016</td>
                    <td>0</td>
                </tr>
                        
</tbody></table>

如有必要,您可以为表格创建不同的样式和模式。

例如,这是另一个模型:

getMessagesFor方法:

public function getMessagesFor(Request $request)
{

    $seasons = [ 2005, 2006,2007,
        2008,
        2009,
        2010,
        2011,
        2012,
        2013,
        2014,
        2015,
        2016];

    $seasonFinal=[];
    foreach($seasons as $session) {
        $seasonFinal[$session]['win']=GameData::where('team', $id)->where('win', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $session)
            ->count();
        $seasonFinal[$session]['loss']=GameData::where('team', $id)->
        where('loss', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $session)
            ->count();
    }


return view('welcome3',compact('seasonFinal'));

}

welcome3视图:

<table border="1">

@if($seasonFinal)
    <tr>
        <td>year  </td>
        <td> win count</td>
        <td> loss count</td>
    </tr>
    @foreach($seasonFinal as $year=>$value)
        <tr>
            <td>{{$year}}</td>
            <td> {{$value['win']}}</td>
            <td> {{$value['loss']}}</td>
        </tr>
    @endforeach
@endif

</table>

结果:

enter image description here

答案 2 :(得分:0)

我能够通过laravel FB页面上的一些帮助解决此问题。...

控制器

            $seasons =( [ 2005, 2006,2007,
            2008,
            2009,
            2010,
            2011,
            2012,
            2013,
            2014,
           2015,
           2016]);



           $seasonFinalWins=[];
           $seasonFinalLosses=[];

    foreach($seasons as $homeFinalWin) {
            $seasonFinalWins[$homeFinalWin]=GameData::where('team', $id)->
            where('win', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $homeFinalWin)
            ->count();
           }

           foreach($seasons as $homeFinalLoss) {
            $seasonFinalLosses[$homeFinalLoss]=GameData::where('team', $id)->
            where('loss', '1')
            ->where('stage', 'R16')
            ->where('round', 'final')
            ->where('year', $homeFinalLoss)
            ->count();
           }

查看

     <th scope="row">Finals Appearances</th>


@if(isset($seasonFinalWins) && isset($seasonFinalLosses))
  @foreach($seasonFinalWins as $key => $sfn)
    <td>{{$sfn}}-{{$seasonFinalLosses[$key]}}</td>
  @endforeach
@endif

注意:不确定带有空数组的变量是否必要,但是帮助我编写此代码的人将其包括在内。