You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel使用back()->withInput()返回旧值填充表格时如何获取actions值

问题排查步骤

  1. 先确认表单字段命名是否正确
    你的actions是数组类型的提交值,对应的表单输入(比如动态生成的隐藏域)的name属性必须带数组后缀,格式为name="actions[]",如果写成name="actions",Laravel无法正确识别数组值,会导致闪存后old()无法读取。
  2. 确认字段没有被排除闪存
    检查控制器返回back()->withInput()之前,是否调用了$request->flashExcept('actions')之类的方法将actions字段排除,或者表单验证规则中actions字段的过滤规则导致值被丢弃。
  3. 验证闪存数据是否正常存入
    可以在控制器返回前临时加调试代码,确认session中是否存在旧值:
dd(session()->get('_old_input'));

如果输出的数组中存在actions键,说明闪存正常,问题出在视图读取环节;如果不存在,优先排查提交和闪存逻辑。


表格渲染代码

确认闪存正常后,直接遍历旧值解码输出即可,Blade代码示例如下:

<table>
  <thead>
    <tr>
      <th>Ação</th>
      <th>O que</th>
      <th>Por que</th>
      <th>Quando</th>
      <th>Quem (interno)</th>
      <th>Quem (externo)</th>
      <th>Onde</th>
      <th>Como</th>
      <th>Ações</th>
    </tr>
  </thead>
  <tbody>
    {{-- 遍历旧的actions数组,默认给空数组避免报错 --}}
    @foreach(old('actions', []) as $actionJson)
      @php
        // 解码JSON字符串为数组,解码失败跳过该行避免报错
        $action = json_decode($actionJson, true);
        if (!$action) continue;
      @endphp
      <tr>
        <td>{{ $action['action'] ?? '' }}</td>
        <td>{{ $action['what'] ?? '' }}</td>
        <td>{{ $action['why'] ?? '' }}</td>
        <td>{{ $action['when'] ?? '' }}</td>
        <td>{{ implode(',', $action['who'] ?? []) }}</td>
        <td>{{ implode(',', $action['external_people'] ?? []) }}</td>
        <td>{{ $action['where'] ?? '' }}</td>
        <td>{{ $action['how'] ?? '' }}</td>
        <td><!-- 此处放你的行操作按钮逻辑 --></td>
      </tr>
    @endforeach
  </tbody>
</table>

如果还是无法通过old('actions')读取,可以用session()->get('_old_input.actions', [])作为兜底替代old('actions'),确认读取逻辑没问题后再排查根因。

内容的提问来源于stack exchange,提问作者Luis de Souza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 19:36:03