2017-08-06 52 views
1

我嘗試構建一個應該創建數組的函數。我想在我看來這個陣列。 這是函數,我不知道如何構建數組。基於從laravel葉片創建數組php和輸出選項值

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); 
    } 

我認爲,這是我的輸出

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]的第一列?在選項數組中,我需要名稱數組[1]。我必須改變,這將工作?

回答

0

我猜u需要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 
0

$ alleSpiele是迭代此對象後數據對象的集合,您應該使用對象名稱而不是數組。你可以嘗試改變的代碼

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