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

Laravel 5.5.28如何修改默认认证字段以解决登录失败问题?

解决Laravel默认认证登录时找不到email字段的问题

你已经精准定位到问题根源了——Laravel默认认证系统会自动以email和password作为登录验证字段,但你的数据库里对应的是con_email和con_password。只需要几个简单的修改就能搞定这个问题:

1. 修改LoginController,指定登录身份字段

在你的LoginController.php中,重写username()方法,告诉Laravel用con_email作为登录时的身份验证字段:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    // 重写username方法,指定登录用的邮箱字段
    public function username()
    {
        return 'con_email';
    }

    // 其他默认代码...
}

2. 在Consumer模型中指定密码字段名称

因为你的密码字段不是默认的password,需要在对应的Consumer模型(比如App\Models\Consumer.php)里设置$password属性,让Laravel知道该用哪个字段验证密码:

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Consumer extends Authenticatable
{
    use Notifiable;

    // 指定自定义的密码字段
    protected $password = 'con_password';

    // 确保fillable包含你的字段(注册正常的话应该已经配置过)
    protected $fillable = [
        'con_email', 'con_password', // 其他需要批量赋值的字段...
    ];

    // 其他模型代码...
}

3. 额外检查:确认auth配置正确性

打开config/auth.php,确保providers配置指向了正确的模型和数据表:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\Consumer::class,
        // 如果表名不是默认的users,可手动指定:
        // 'table' => 'consumers',
    ],
],

最后可以清除一下配置缓存(可选但推荐):

php artisan config:clear

现在再测试登录功能,应该就能正常验证con_email和con_password了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:33:12