【问题标题】:Updating record in a dynamically generated table in Laravel?在 Laravel 中更新动态生成的表中的记录?
【发布时间】:2017-04-26 20:54:40
【问题描述】:

我正在尝试在电子商务网站中建立一个员工区域,允许员工查看可用产品表,编辑他们的详细信息并通过单击按钮进行更新。

目前我在使用实际更新功能时遇到了问题,因为我的请求输入一直返回 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>

以及新的控制器代码:

 /**
 * 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');
}

【问题讨论】:

  • 不确定这是否允许 $request->product_name_.$id,使用 sprintf('%s_%s', $request->product_name, $id) 连接它。
  • 是的,我很确定这是不允许的,但这是我最后一次尝试我还没有尝试过的东西
  • 您可以将输入名称指定为array,将product_id 指定为key
  • 点赞:name="cost[{{$product-&gt;id}}]"
  • 很好,我会试试看是否有帮助

标签: php laravel


【解决方案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>

然后您将使用RequestInput::get() 获得值

【讨论】:

  • 有没有办法有效地将输入绑定到特定产品?我知道 $request->product_name_.$id 行不通
【解决方案2】:

试试这个:

@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

【讨论】:

    猜你喜欢
    • 2014-06-07
    • 1970-01-01
    • 2018-11-22
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多