无需自定义代码修改Laravel Fortify用户表email字段为email_id
解决方案:无需自定义代码修改Laravel Fortify适配
email_id字段 步骤1:修改config/fortify.php核心配置
找到你看到的「Username / Email」配置块,添加或修改以下两个配置项:
/* |-------------------------------------------------------------------------- | Username / Email |-------------------------------------------------------------------------- | | This value defines which model attribute should be considered as your | application's "username" field. Typically, this might be the email | address of the users but you are free to change this value here. | | Out of the box, Fortify expects forgot password and reset password | requests to have a field named 'email'. If the application uses | another name for the field you may define it below as needed. | */ 'username' => 'email_id', 'email' => 'email_id',
username:指定Fortify验证登录时匹配的用户表字段(替换默认的email)email:指定找回/重置密码流程中,请求参数与用户表对应的字段(确保和前端表单提交的字段名一致)
步骤2:确保User模型字段可填充
在app/Models/User.php中,将email_id加入$fillable数组,避免批量赋值报错:
protected $fillable = [ 'name', 'email_id', 'password', ];
步骤3:调整前端表单字段名
如果你使用Fortify默认的认证视图(或自定义视图),需要把所有涉及邮箱的输入框name属性改为email_id:
- 登录表单:将
<input name="email">改为<input name="email_id"> - 找回密码表单:同样修改输入框的
name为email_id - 注册表单(若启用):对应字段也改为
email_id
完成以上配置后,Fortify会自动使用email_id字段处理登录、找回密码、重置密码的全流程,完全不需要编写自定义的authenticateUsing逻辑,也不用修改其他核心认证代码。
内容的提问来源于stack exchange,提问作者Nishant Patel
相关产品推荐
相关产品推荐

