Laravel查看代码:
<tbody>
@foreach($drycleaning as $drycleaning)
<tr>
<td class=""><input type="text" size="5" name="dc_{{$drycleaning->id}}"></td>
<td class="">
<div><div class="form-group">
<select id="projectinput5" name="p_dc_{{$drycleaning->id}}" class="form-control">
<option value="" selected>Price of</option>
<option name="p_dc_{{$drycleaning->id}}" value="{{$prices->shirt}}">shirt</option>
<option name="p_dc_{{$drycleaning->id}}" value="{{$prices->pant}}">pant</option>
<option name="p_dc_{{$drycleaning->id}}" value="{{$prices->top}}">top</option>
</select>
</div></div>
</td>
</tr>
@endforeach
</tbody>
控制器代码:
public function store(Request $request)
{
$bill = Bill::create([
'waf_1' => $request->waf_1,
'dc_1' => $request->dc_1,
'dc_2' => $request->dc_2,
'dc_3' => $request->dc_3,
...
'p_dc_1' => $request->p_dc_1,
'p_dc_2' => $request->p_dc_2,
'p_dc_3' => $request->p_dc_3,
...
]);
$bill->save();
return redirect()->route('sp.orders.index');
}
由于foreach在视图中,输入和下拉选择显示3次。并且只有3个输入的输入值成功保存到数据库中,但未保存到所选的选项值中。 任何人都可以帮我理解我的代码错误吗?
答案 0 :(得分:0)
我会将名称属性设为一个数组,因为这样你可以拥有任意数量的数据,而且你的id并不总是一样。希望这会让你前进:
@foreach($drycleaning as $drycleaning)
<tr>
<td class=""><input type="text" size="5" name="dc[]"></td>
<td class="">
<div><div class="form-group">
<select id="projectinput5" name="p_dc[]" class="form-control">
<option value="" selected>Price of</option>
<option value="{{$prices->shirt}}">shirt</option>
<option value="{{$prices->pant}}">pant</option>
<option value="{{$prices->top}}">top</option>
</select>
</div></div>
</td>
</tr>
@endforeach
然后你可以在你的控制器中迭代
答案 1 :(得分:0)
这是适合你的解决方案
在您的刀片中
<tbody>
@foreach($drycleaning as $drycleaning)
<tr>
<td class=""><input type="text" size="5" name="dc[{{ $drycleaning->id }}]"></td>
<td class="">
<div><div class="form-group">
<select id="projectinput5" name="p_dc[{{ $drycleaning->id }}]" class="form-control">
<option value="" selected>Price of</option>
<option value="{{ $prices->shirt }}">shirt</option>
<option value="{{ $prices->pant }}">pant</option>
<option value="{{ $prices->top }}">top</option>
</select>
</div></div>
</td>
</tr>
@endforeach
</tbody>
在您的控制器中
public function store(Request $request)
{
$data = array();
$data['waf_1'] = $request->waf_1;
foreach ($request->dc as $key => $value)
$data['dc_'.$key] = $value;
foreach ($request->p_dc as $key => $value)
$data['p_dc_'.$key] = $value;
$bill = Bill::create($data);
return redirect()->route('sp.orders.index');
}