You need to enable JavaScript to run this app. Laravel Inertia表单提交无响应,VerificationController数据与图片存入失败
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Inertia表单提交无响应,VerificationController数据与图片存入失败

问题根因定位与修正方案

核心现象说明

你在控制器中添加的dd()调试无输出、提交请求后页面无反应,本质是请求在进入业务逻辑前就被拦截返回,且前端未渲染后端返回的错误信息,形成“请求无响应”的假象。

后端代码存在的硬错误

  • 数据更新逻辑错误:你通过DB::table('users')->where(...)->first()获取的是原生stdClass对象,不具备Eloquent模型的save()方法,调用该方法会直接触发PHP致命错误。
  • 事务语法与逻辑错误:
    • 代码中写的DB:commit()是PHP语法错误,正确的静态调用写法需要双冒号,即DB::commit()
    • 代码全程没有调用DB::beginTransaction()开启事务,即使修正语法,提交未开启的事务也会触发运行时报错。
  • 验证拦截逻辑:所有dd()调试代码都写在验证逻辑之后,当验证不通过时,程序会直接返回验证错误,不会执行到后续的调试代码,自然看不到输出。

前端代码存在的逻辑错误

  • 文件字段未正确初始化:你的data()返回值中仅定义了verificationForm,没有单独定义响应式的id_front、id_back变量,导致初始化表单时这两个字段的值为undefined,根本不是有效的File对象。
  • 未绑定文件选择事件:没有将文件选择input的选中值赋值给表单的对应字段,后端接收到的请求中不包含有效文件,直接触发“文件为必填项”的验证规则。
  • 错误展示缺失:你配置的错误bag是createVerification,但模板中没有绑定对应错误的渲染逻辑,后端返回的422验证错误被前端静默忽略,形成“无反应”的错觉。
  • 你在Chrome开发者工具中看到的报错内容翻译为:接口返回422验证失败状态,提示id_front、id_back为必填字段,请求未携带有效上传文件数据。

修正后的参考代码

后端控制器修正版

public function store(Request $request) 
{
    // 开启数据库事务
    \DB::beginTransaction();
    try {
        // 表单验证
        $validated = $request->validate([
          'firstname' => 'required|min:2|max:255',
          'lastname' => 'required|min:2|max:255',
          'country' => 'required|string',
          'id_front' => 'required|image|mimes:jpg,jpeg,png|max:2048',
          'id_back' => 'required|image|mimes:jpg,jpeg,png|max:2048',
        ]);

        // 存储文件
        $user = auth()->user();
        $path_front = $request->file('id_front')->store('user_verifications/front/'.$user->id);
        $path_back = $request->file('id_back')->store('user_verifications/back/'.$user->id);

        // 更新用户表数据
        \DB::table('users')->where('id', $user->id)->update([
            'verified' => 'yes'
        ]);

        // 创建核验记录
        Verification::create([
            'user_id' => $user->id,
            'firstname' => $validated['firstname'],
            'lastname' => $validated['lastname'],
            'country' => $validated['country'],
            'id_front_path' => $path_front,
            'id_back_path' => $path_back,
        ]);

        \DB::commit();
        return redirect()->back()->with('success', '核验提交成功');
    } catch (\Exception $e) {
        \DB::rollBack();
        return back()->withErrors(['submit_error' => $e->getMessage()]);
    }
}

前端Vue修正版

data() {
    return {
        // 单独定义文件选择的响应式变量
        id_front: null,
        id_back: null,
        verificationForm: this.$inertia.form({
            firstname: '',
            lastname: '',
            country: 'USA',
            id_front: null,
            id_back: null,
            options: [
                // 原有的国家选项数组保留即可
            ]
        }, {
            bag: 'createVerification',
            resetOnSuccess: false,
        }),
    }
},
methods: {
    // 处理文件选择
    handleFileChange(field, event) {
        this.verificationForm[field] = event.target.files[0];
    },
    createVerification() {
        // Inertia会自动识别文件类型,设置multipart传输
        this.verificationForm.post('/user/verify', {
            preserveScroll: true,
            forceFormData: true, // 强制使用表单数据传输,适配文件上传
        });
    }
}

模板部分需要注意:

  • 表单标签上添加@submit.prevent="createVerification"绑定提交事件
  • 文件选择input上分别添加@change="handleFileChange('id_front', $event)"、@change="handleFileChange('id_back', $event)"
  • 模板中添加错误展示块,绑定$page.errors.createVerification的错误内容,确保验证失败时能在页面上看到提示。

内容的提问来源于stack exchange,提问作者Obchod Hraček

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.16 20:54:04