首页 文章

Laravel默认身份验证 - 添加字段会破坏登录能力

提问于
浏览
0

我坚持使用默认的Laravel Auth . 我通过 php artisan make:auth 生成了它 .

我的用户表需要的字段多于默认提供的字段 . 这是我对表的迁移 .

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first', 20)->nullable();
            $table->string('last', 20)->nullable();
            $table->string('email', 50)->unique();
            $table->string('password', 20);
            $table->integer('userRoleId')->unsigned()->index();
            $table->integer('userGroupId')->unsigned()->index();
            $table->integer('companyId')->unsigned()->index();
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('users');
    }
}

然后我编辑了 User.php 模型并更新了 $fillable 字段 .

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable = [
        'first', 'last', 'email', 'password', 'userRoleId', 'userGroupId', 'companyId',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];
}

当我提交表单时,它会正确地发布到数据库并存储 . 我可以看到这一行 .

问题是,现在当我创建一个新用户并返回 login 并填写表单后,它返回错误,指出我输入的电子邮件地址与任何记录都不匹配,即使显然有一行具有相同的数据和数据库中的相同列名称 .

我究竟做错了什么?

1 回答

  • 0

    @The Alpha在评论中的答案解决了我的问题 .

    我的过程是正确的,除了我没有考虑到密码被哈希并且没有足够的字符可用于我的数据库字段中的哈希 .

    $table->string('password', 20); 需要更改为 $table->string('password', 60);

相关问题