我在Laravel中通过POST执行记录更新阵列时遇到问题。 我已经捕获了数组无法更新数组成就中的所有帖子数据
<form action="{{'updateapp'}}" method="post">
<table>
<tr><td>
<input type="checkbox" class="id" name="id[]" value="{{ $quarter->id }}" />
<input type="text" name="achv[]" value="{{ $quarter->achievement }}">
</td></tr>
</table>
</form>
控制器:
public function foo(Request $request)
{
$ids = $request->id;
$achvs = $request->achv;
DB::table('quarters')->whereIn('id', $ids)
->update(array(['achievement' => $achvs ]));
return redirect('evaluator');
}
答案 0 :(得分:3)
由于您在表单中设置了[]数组,因此可以按照以下方式访问它
public function foo(Request $request)
{
$ids = $request->id[0];
$achvs = $request->achv[0];
DB::table('quarters')->where('id', $ids)
->update(['achievement' => $achvs ]);
return redirect('evaluator');
}
如果要更新多行,请使用以下代码:
foreach($request->id as $key => $value){
$quarters = Quarters::find($request->id[$key]);
$quarters->achievement = $request->achv[$key];
$quarters->save();
}
答案 1 :(得分:0)
public function foo(Request $request)
{
$ids = $request->id[0];
$achvs = $request->achv[0];
DB::table('quarters')->where('id', $ids)
->update(array(['achievement' => $achvs,'achievement1' => $achvs1]));
return redirect('evaluator');
}