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

Laravel密码重置报错:'email'字段不能为空

Laravel密码重置出现SQL约束错误:email字段不能为空

错误详情

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null (Connection: main, SQL: insert into password_resets (email, token, created_at) values (?, $2y$10$sFSxC9pgIbmRisHh4RZK5.j0cZNLlmogdlbiokfnKDLoSv4ng.SHW, 2024-01-29 14:44:11))

相关代码片段

密码重置邮件发送函数

public function sendResetPasswordEmail(Request $request)
{
    $request->validate(['email' => 'required|email']);

    $email = $request->input('email');

    $status = Password::sendResetLink(['email' => $email]);

    return $status === Password::RESET_LINK_SENT
        ? back()->with(['status' => __($status)])
        : back()->withErrors(['email' => __($status)]);
}

POST请求体

{
    "email": "ilovato@example.com"
}

已尝试的调试操作

  • 确认请求体中存在email字段且已被正确提取
  • 验证Laravel版本为最新且符合官方文档要求
  • 将password_resets表的email字段设为可空,但仍存入null值

解决建议

  1. 确认请求解析是否正常
    确保请求头包含Content-Type: application/json,Laravel默认仅解析带该头的JSON请求。如果请求头不正确,$request->input('email')会获取不到值返回null。可以尝试用$request->json('email')直接获取JSON字段,或改用表单提交格式(x-www-form-urlencoded)测试。

  2. 简化参数传递
    替换参数传递方式,直接使用$request->only('email'),避免手动赋值可能出现的问题:

    $status = Password::sendResetLink($request->only('email'));
    
  3. 强制调试确认值
    在函数中加入调试代码,确认$email变量的实际值:

    $email = $request->input('email');
    dd($email); // 查看输出是否为预期的邮箱地址
    

    如果输出为null,说明请求数据未被正确解析,重点排查请求格式和头信息。

  4. 检查密码重置表迁移文件
    确保password_resets表的迁移文件中,email字段定义正确,示例:

    Schema::create('password_resets', function (Blueprint $table) {
        $table->string('email')->index();
        $table->string('token');
        $table->timestamp('created_at')->nullable();
    });
    

    如果之前修改过字段属性,执行php artisan migrate:fresh(注意备份数据)重新生成表结构。

内容的提问来源于stack exchange,提问作者Pedro Ortiz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 08:31:17