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值
解决建议
确认请求解析是否正常
确保请求头包含Content-Type: application/json,Laravel默认仅解析带该头的JSON请求。如果请求头不正确,$request->input('email')会获取不到值返回null。可以尝试用$request->json('email')直接获取JSON字段,或改用表单提交格式(x-www-form-urlencoded)测试。简化参数传递
替换参数传递方式,直接使用$request->only('email'),避免手动赋值可能出现的问题:$status = Password::sendResetLink($request->only('email'));强制调试确认值
在函数中加入调试代码,确认$email变量的实际值:$email = $request->input('email'); dd($email); // 查看输出是否为预期的邮箱地址如果输出为null,说明请求数据未被正确解析,重点排查请求格式和头信息。
检查密码重置表迁移文件
确保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
相关产品推荐
相关产品推荐

