我正在尝试在电子商务网站中建立一个员工区域,允许员工查看可用产品表,编辑他们的详细信息并通过点击按钮进行更新。
目前我遇到了实际更新功能的问题,因为我的请求输入已返回null,这意味着查询构建器失败。我似乎也无法找到一种唯一的方法来唯一地识别我的表中的每个输入,这可能是主要问题。
编辑问题已解决。表单标签必须包装每一行,并感谢所有给出输入数组建议的人。代码如下所示:
<div class="container">
<div class="jumbotron text-center clearfix">
@foreach($products as $index => $product)
@if ($index == 0)
<h2>{{$product->category}} Products </h2>
@endif
@endforeach
</div>
<table id="orders" class="table table-striped table-inverse">
<thead>
<tr id='tableHeader'>
<td>ID</td>
<td>Product Name</td>
<td>Slug</td>
<td>Quantity</td>
<td>Cost (£)</td>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<form action="{{ url('staff/storeUpdateProduct', [$product->id]) }}" method="POST" class="side-by-side">
{!! csrf_field() !!}
<td><input id="{{ $product->id}}" type="text" class="form-control" name="id[{{ $product->id}}]" value="{{ $product->id }}" readonly></td>
<td><input id="product_name[{{ $product->id}}]" type="text" class="form-control" name="product_name[{{ $product->id}}]" value="{{ $product->product_name }}"></td>
<td><input id="slug[{{ $product->id}}]" type="text" class="form-control" name="slug[{{ $product->id}}]" value="{{ $product->slug }}"></td>
<td><input id="quantity[{{ $product->id}}]" type="text" class="form-control" name="quantity[{{ $product->id}}]" value="{{ $product->quantity}}"></td>
<td><input id="cost[{{ $product->id}}]" type="text" class="form-control" name="cost[{{ $product->id}}]" value="{{ $product->cost }}"></td>
<td>
<input type="submit" class="btn btn-sm" value="Update">
</td>
</form>
</tr>
@endforeach
</tbody>
新的Controller代码:
/**
* Updates product details.
*
* @param Request $request
* @param String $id
* @return \Illuminate\Http\Response
*/
public function storeUpdateProduct(Request $request, $id)
{
$product = Product::where('id', '=', $id)->firstOrFail();
/*dd([
$request->product_name[$id],
$request->slug[$id],
$request->quantity[$id],
$request->cost[$id]
]);*/
$product->product_name = $request->product_name[$id];
$product->slug = $request->slug[$id];
$product->quantity = $request->quantity[$id];
$product->cost = $request->cost[$id];
$product->save();
return redirect()->route('staff.updateProducts');
}
答案 0 :(得分:1)
问题在于:
<form action="{{ url('staff/storeUpdateProduct', [$product->id]) }}" method="POST" class="side-by-side">
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm" value="Update">
</form>
submit
标记内只有form
按钮。您必须将所有input
以及submit
按钮放在表单中,如:
<form action="{{ url('staff/storeUpdateProduct', [$product->id]) }}" method="POST" class="side-by-side">
{!! csrf_field() !!}
@foreach ($products as $product)
<tr>
<td><input id="{{ $product->id}}" type="text" class="form-control" name="{{ $product->id}}" value="{{ $product->id }}" readonly></td>
<td><input id="product_name_{{ $product->id}}" type="text" class="form-control" name="product_name_{{ $product->id}}" value="{{ $product->product_name }}"></td>
<td><input id="slug_{{ $product->id}}" type="text" class="form-control" name="slug_{{ $product->id}}" value="{{ $product->slug }}"></td>
<td><input id="quantity_{{ $product->id}}" type="text" class="form-control" name="quantity_{{ $product->id}}" value="{{ $product->quantity}}"></td>
<td><input id="cost_{{ $product->id }}" type="text" class="form-control" name="cost_{{ $product->id }}" value="{{ $product->cost }}"></td>
<td>
<input type="submit" class="btn btn-sm" value="Update">
</td>
</tr>
@endforeach
</form>
然后您将使用Request
或Input::get()
答案 1 :(得分:1)
试试这个:
@foreach ($products as $product)
<tr>
<td><input id="{{ $product->id}}" type="text" class="form-control" name="{{ $product->id}}" value="{{ $product->id }}" readonly></td>
<td><input id="product_name_{{ $product->id}}" type="text" class="form-control" name="product_name[{{ $product->id}}]" value="{{ $product->product_name }}"></td>
<td><input id="slug_{{ $product->id}}" type="text" class="form-control" name="slug[{{ $product->id}}]" value="{{ $product->slug }}"></td>
<td><input id="quantity_{{ $product->id}}" type="text" class="form-control" name="quantity[{{ $product->id}}]" value="{{ $product->quantity}}"></td>
<td><input id="cost_{{ $product->id }}" type="text" class="form-control" name="cost[{{ $product->id }}]" value="{{ $product->cost }}"></td>
<td>
<form action="{{ url('staff/storeUpdateProduct', [$product->id]) }}" method="POST" class="side-by-side">
{!! csrf_field() !!}
<input type="submit" class="btn btn-sm" value="Update">
</form>
</td>
</tr>
@endforeach