为什么只能更新我的第一行?在此形式中,循环5次

<form action="{{ route('detilpermintaan.update') }}" name="Patk" id="Patk" method="POST" style="display: inline-block;"
      class="was-validated">
    @csrf
    @method('PUT')
    <div class="input-group input-group-sm mb-3">
        <input type="number" name="id[]" value="{{ $b->id }}" hidden>
        <input type="number" class="form-control" aria-label="Sizing example input"
               aria-describedby="inputGroup-sizing-sm" name="satuan_permintaan[]" value="{{ $b->satuan_permintaan }}"
               min="1" max={{ $b->stok }} required>
        <span class="input-group-text" id="inputGroup-sizing-sm">{{ $b->subsatuan_atk }}</span>
    </div>
</form>

Blade/View

@php $no = 1; @endphp
@forelse ($permintaans as $b)
    <tr>
        <td>{{ $no ++ }}</td>
        <td>
            {{ $b->nama_kategori }} {{ $b->nama_atk }}
        </td>
        <td>
            <form action="{{ route('detilpermintaan.update') }}" name="Patk" id="Patk" method="POST"
                  style="display: inline-block;" class="was-validated">
                @csrf
                @method('PUT')
                <div class="input-group input-group-sm mb-3">
                    <input type="number" name="id[]" value="{{ $b->id }}" hidden>
                    <input type="number" class="form-control" aria-label="Sizing example input"
                           aria-describedby="inputGroup-sizing-sm" name="satuan_permintaan[]"
                           value="{{ $b->satuan_permintaan }}" min="1" max={{ $b->stok }} required>

                    <span class="input-group-text" id="inputGroup-sizing-sm">{{ $b->subsatuan_atk }}</span>
                </div>
            </form>
        </td>
        <td>
            <form action="{{ route('detilpermintaan.destroy', $b->id) }}" method="POST" style="display: inline-block;">
                @csrf
                @method('DELETE')
                <button type="submit" class="btn custom2-btn"></button>
            </form>
        </td>
    </tr>
    @empty
    @endforelse
    </tbody>
    </table>
    </div>
    <div class="card-footer text-muted">
        @if ($permintaans->isEmpty())
        @else
            <div class="alert alert-warning" role="alert">
                <h6><b> jenis barang dengan quantity sebanyak item </b></h6>
                <button type="submit" value="Update" class="btn btn-success btn-sm" onclick="submitForms()"><i
                            class="bi bi-floppy2-fill"></i> Simpan
                </button>
            </div>
        @endif

        <script>
            function submitForms() {
                var forms = document.getElementsByName("Patk");
                forms.forEach(function (form) {
                    form.submit();
                });
                document.getElementById('Patk').submit();
            }
        </script>

Controller

public function update(Request $request)
{
    try {
        DB::beginTransaction();
        
        for ($i = 0; $i < count($request->input('id')); $i++) {
            $id = $request->id[$i];
            Detilpermintaan::where('id', $id)->update([
                'satuan_permintaan' => $request->satuan_permintaan[$i],
                'permintaan_id' => $id_permintaan,
                // Add other fields as needed
            ]);
        }

        DB::commit();
    } catch (\Exception $e) {
        DB::rollback();
        // Handle the exception
        dd($e->getMessage()); // Output the error message for debugging
        return response()->json(['error' => 'Error updating records.']);
    }
    
    return redirect()->route('detilpermintaan.index');
}

更新ID所在位置的数据

推荐答案

您的Blade 视图中有多个表单.当您在submitForms()中调用form.submit()时,每个表单都会被提交,从而产生多个HTTP请求,每个请求只携带一个表单的数据.这就是为什么只更新第一行的原因.要解决这个问题,您应该只有一个表单来包装您的所有输入域,然后提交该表单.

@php $no = 1; @endphp
<form action="{{ route('detilpermintaan.update') }}" name="Patk" id="Patk" method="POST" style="display: inline-block;" class="was-validated">
    @csrf
    @method('PUT')
    @forelse ($permintaans as $b)
        <tr>
            <td>{{ $no ++ }}</td>
            <td>
                {{ $b->nama_kategori }} {{ $b->nama_atk }}
            </td>
            <td>
                <div class="input-group input-group-sm mb-3">
                    <input type="number" name="id[]" value="{{ $b->id }}" hidden>
                    <input type="number" class="form-control" aria-label="Sizing example input" aria-describedby="inputGroup-sizing-sm" name="satuan_permintaan[]" value="{{ $b->satuan_permintaan }}" min="1" max={{ $b->stok }} required>
                    <span class="input-group-text" id="inputGroup-sizing-sm">{{ $b->subsatuan_atk }}</span>
                </div>
            </td>
            <td>
                <button type="button" class="btn custom2-btn" onclick="submitDeleteForm('deleteForm{{ $b->id }}')"></button>
            </td>
        </tr>
    @empty
    @endforelse
    <button type="submit" value="Update" class="btn btn-success btn-sm"><i class="bi bi-floppy2-fill"></i> Simpan</button>
</form>

@forelse ($permintaans as $b)
    <form action="{{ route('detilpermintaan.destroy', $b->id) }}" id="deleteForm{{ $b->id }}" method="POST" style="display: none;">
        @csrf
        @method('DELETE')
    </form>
@endforelse

<script>
    function submitDeleteForm(formId) {
        document.getElementById(formId).submit();
    }
</script>

删除submitForms()函数...你不需要它.

Php相关问答推荐

自定义WooCommerce查询字符串仅显示前3个产品

与会者在WooCommerce中按购物车项目数量自定义 checkout 字段

获取要删除的图像的公共ID

EBay Inventory API createOrReplaceInventoryItem出错

使用PHP curl的Amazon SP API批处理调用

筛选器具有多个查询之一

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

如何将WooCommerce产品从一个产品复制到另一个产品S

PHP 8.2 弃用日志(log)存储在 Laravel 10.22.0 中的哪里?

奇怪的 preg_match_all() 行为

我不明白为什么我收到未定义的数组键异常错误 - Laravel 9 PHP 8

将自定义保存金额移至 Woocommerce 简单产品中的价格以下

Golang - Html 模板:如何创建导航项 - 子菜单?我找不到怎么做

在 groupBy laravel 5.7 中 Select 多列

如何使用 WhatsApp Cloud API 向 WhatsApp 发送消息,而无需在发送消息之前注册收件人号码?

php preg_match 验证 Tiktok 用户名

如何用 php ML 解决这个问题?

Shopware 6:无法在新的自定义插件中保存关联字段(媒体)

如何故意创建不同的错误(404,500,419)? Lavravel PHP

复杂的mysql查询问题