我有多个复选框,可以从数据库中检索这些值。每个复选框都有一个选择/下拉列表,用户可以将其更改为low, medium, high
并更新值。我的问题是更新不起作用,下拉/选择只返回选项中的第一项。用户应该能够一次更改多个项目。
查看
<form method="post" action="{{ route('allergies.update', ['id'=>Auth::user()->id]) }}" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
@foreach($userallergens as $aller)
<br>
<input type="hidden" name="ua_id" value="{{$aller->ua_id}}">
<select name="tolerance[]">
<option value="Low">Low</option>
<option value="Medium">Medium</option>
<option value="High">High</option>
</select>
<label style="color:black" name="allergen[]" value="{{$aller->ua_id}}">
{{ $aller->allergen_name}}
</label>
@endforeach
</form>
控制器
<?php
public function update2(Request $request, $id)
{
$user = User::find($id);
for ($j = 0; $j < count($request['tolerance']); $j++) {
$user = UserAllergen::where('ua_id', $request['ua_id'])
->where('user_id', $user)
->update(['tolerance_level' => $request['tolerance'][$j]]);
}
return redirect()->route('user.profile', compact('id', 'user'));
}
答案 0 :(得分:0)
试试这段代码,希望它会对你有帮助。
public function update2(Request $request, $id){
$user = Auth::user()->id;
$data = $request->all();
$tolerances = $data['tolerance'];
foreach($tolerances as $tolerance)
{
$this->updateData($data['ua_id'], $user, $tolerance);
}
return redirect()->route('user.profile', compact('id', 'user'));
}
public function updateData($ua_id, $user, $tolerance_level)
{
UserAllergen::updateOrCreate(
['ua_id' => $ua_id, 'user_id' => $user],
[
'tolerance_level' => $tolerance_level
]
);
}
答案 1 :(得分:0)
添加多个属性以选择多个项目。
<select name="tolerance[]" multiple>