【问题标题】:Button can't save first item when clicked单击按钮时无法保存第一项
【发布时间】:2020-11-06 00:00:24
【问题描述】:

foreach 循环中,我有多个项目,每个项目都有一个名为“保存”的按钮。但是,当我单击第一个“保存”按钮时,它只保存数据库中的最后一项。

@foreach($product_dr as $product)
    <tr>
        <td style="font-size: 20px;color: #101010">
            <div class="form-group">
                <input type="hidden" name="title" value="{{$product->title}}">
                <label for="inputText3" class="col-form-label"></label>
                {{$product->title}}
            </div>
        </td>
        <td style="font-size: 20px;color: #2ec551">
            <div class="form-group">
                <input id="inputText4"   VALUE="{{$product->price}}" type="text" name="price" class="form-control" placeholder="Amount" style="width: 100px">
            </div>
        </td>
        <td style="font-size: 20px;color: #2ec551">
            <div class="form-group">
                <input id="inputText4"  VALUE="1" type="number" name="amount" class="form-control" placeholder="Amount" style="width: 100px">
            </div>
        </td>
        <td style="font-size: 20px;color: #2ec551">
            <div class="form-group">
                <input type="hidden" name="table" value="{{$table[0]->number}}">
                <input type="hidden" name="table_number" value="{{$table[0]->number}}">
                <label for="inputText3" class="col-form-label"></label>
                {{$table[0]->number}}
            </div>
        </td>
        <td>
            <div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12 ">
                <button class="btn btn-success" type="submit">Save</button>
            </div>
        </td>
    </tr>
@endforeach

【问题讨论】:

标签: laravel


【解决方案1】:

变量名必须是这样的数组:

<input type="hidden" name="title[]" value="{{$product->title}}">

这样,您将拥有一个可以迭代的数组。如果没有,你只会得到一个“title”的值

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多