我正在使用laravel的每个标头更新标头数量,请在下面帮助我的表,表格,视图和控制器更新功能代码
查看添加头名称
div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
<form action="/headers" method="POST">
@csrf
<div class="row">
<div class="col">
<label for="">Header Name</label>
<input type="text" name="header_name" required="required">
<input type="submit" class="btn btn-primary" name="submit">
</div>
</div>
</form>
</div>
查看添加总金额
<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
<form action="/headers" method="POST">
@csrf
@foreach ($data as $header)
<div class="row">
<div class="col">
<label for="">Header Name</label>
<input type="text" name="header_name[]" value="{{$header->header_name}}" required="required" readonly="readonly">
<input type="number" min="0" name="header_amount[]" placeholder="Amount" required="required">
</div>
</div>
@endforeach
<input type="submit" class="btn btn-primary" name="submit1">
</form>
</div>
控制器功能
$count = Header::count();
for ($i = 1; $i <= $count; $i++)
{
$header = Header::find($i);
$header->header_amount = $request->input('header_amount');
$header->save();
}
exit();
答案 0 :(得分:1)
尝试以下代码:
$count = Header::count();
$header = Header::find($i);
foreach ($header as $key => $detail) {
$header->header_amount = $request->input('header_amount')[$key];
$detail->save();
}
我希望这会有所帮助。
答案 1 :(得分:1)
我建议添加标头ids[]
作为隐藏输入,这样一来您就不必一开始就获得所有Header
,这将减少一个查询。
<div class="row">
<div class="col">
<input type="hidden" name="ids[]" value="{{ $header->id }}">
<label for="">Header Name</label>
<input type="text" name="header_name[]" value="{{$header->header_name}}" required="required" readonly="readonly">
<input type="number" min="0" name="header_amount[]" value="{{ $header->header_amount }}" placeholder="Amount" required="required">
</div>
</div>
然后在您的控制器中,您可能会遇到类似这样的情况:
$amounts = request('header_amount');
foreach (request('ids') as $key => $id) {
Header::where('id', $id)->update(['header_amount' => $amounts[$key]]);
}
答案 2 :(得分:0)
从标题表中获取所有数据,并使用foreach循环更新每个标题行数
$header = Header::all();
foreach ($header as $key => $detail)
{
$detail->header_amount = $request->input('header_amount')[$key];
$detail->save();
}
exit();