创建数组php和输出选项值

时间:2017-08-06 09:32:22

标签: php arrays html-select blade

我尝试构建一个应该创建数组的函数。我希望在我的视图中提出这个数组。 这是函数,我不知道如何构建数组。

 public function getSpielplan(){
        //$newdata =  array (

        $spieltagSpiel = Spielplan::where('Spieltag', '=', 1)->get();
            foreach($spieltagSpiel as $spieltagSpielOutput){

                $heimName = Verein::where('V_ID', '=', $spieltagSpielOutput->Heimmannschaft)->get();
                    foreach($heimName as $heimNameOutput){

                        $gastName = Verein::where('V_ID', '=', $heimNameOutput->Gastmannschaft)->get();
                            foreach($gastName as $gastNameOutput){

                                //array ($spieltagSpielOutput->Spielplan_ID, $heimNameOutput->Name, $gastNameOutput->Name)
                            }
                    }
            }
        //);
        //return view('spielplan')->with('alleSpiele', $newdata);
    }

在我看来,基于laravel的刀片,这将是我的输出

div class="col-xs-6">
                label for="">Spielauswahl/label>
                select class="form-control input-sm" name="spiele" id="spiele">

                @foreach($alleSpiele as $alleSpieleOutput)
    
                    option value="{!! HERE MUST BE SPIELPLAN_ID [array0?]!!}">{{HERE MUST BE NAME [array1?] }}/option>
    
                @endforeach
                /select>
            /div>

在值必须是Spielplan_ID时,我认为必须是数组[0]的第一列?在选项数组中我需要Name数组[1]。我必须改变一下这会起作用吗?

2 个答案:

答案 0 :(得分:0)

$ alleSpiele是迭代此对象后的数据对象的集合,应该使用对象名而不是数组。您可以尝试更改此部分代码

@foreach($alleSpiele as $alleSpieleOutput)
        
  <option value="{{ $alleSpieleOutput->SPIELPLAN_ID }}">
   {{$alleSpieleOutput->Name }}
  </option>
        
@endforeach

答案 1 :(得分:0)

我猜你需要Spielplan_ID作为选项值,选项名称应该与heimName和gastName结合使用吗?我认为V_ID是Verein模型的主键。如果我是对的,请遵循这些代码。

public function getSpielplan(){

  $spieltagSpiel = Spielplan::where('Spieltag', '=', 1)->get();

  foreach($spieltagSpiel as $spieltagSpielOutput){

   $heimName=Verein:: where('V_ID','=',$spieltagSpielOutput->Heimmannschaft)->first();

   $gastName = Verein:: where('V_ID', '=', $heimNameOutput->Gastmannschaft)->first();

   $resultData[$spieltagSpielOutput->Spielplan_ID] =  $heimName->Name. $gastName->Name;

  }

   return view('spielplan')->with('alleSpiele', $resultData);

}

刀片视图应该是这样的,

 @foreach( $alleSpiele as $alleSpieleKey => $alleSpieleName )
   <option value="{{ $alleSpieleKey }}">
   {{ $alleSpieleName }}
   </option>
 @endforeach