创建数组 php 并输出选项值
Create array php and output option value
我尝试构建一个应该创建数组的函数。这个数组我想放在我的视图中。
这是函数,我不确定如何构建数组。
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 的 blade,这将是我的输出
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]。我必须改变什么才能起作用?
$alleSpiele 是迭代此对象后的数据对象的集合,您应该使用对象名称而不是数组。您可以尝试更改这部分代码
@foreach($alleSpiele as $alleSpieleOutput)
<option value="{{ $alleSpieleOutput->SPIELPLAN_ID }}">
{{$alleSpieleOutput->Name }}
</option>
@endforeach
我猜你需要 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);
}
blade 视图应该是这样的,
@foreach( $alleSpiele as $alleSpieleKey => $alleSpieleName )
<option value="{{ $alleSpieleKey }}">
{{ $alleSpieleName }}
</option>
@endforeach
我尝试构建一个应该创建数组的函数。这个数组我想放在我的视图中。 这是函数,我不确定如何构建数组。
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 的 blade,这将是我的输出
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]。我必须改变什么才能起作用?
$alleSpiele 是迭代此对象后的数据对象的集合,您应该使用对象名称而不是数组。您可以尝试更改这部分代码
@foreach($alleSpiele as $alleSpieleOutput)
<option value="{{ $alleSpieleOutput->SPIELPLAN_ID }}">
{{$alleSpieleOutput->Name }}
</option>
@endforeach
我猜你需要 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);
}
blade 视图应该是这样的,
@foreach( $alleSpiele as $alleSpieleKey => $alleSpieleName )
<option value="{{ $alleSpieleKey }}">
{{ $alleSpieleName }}
</option>
@endforeach