Php Laravel 5.4,重命名用户表列

Php Laravel 5.4,重命名用户表列,php,laravel,authentication,laravel-5.4,Php,Laravel,Authentication,Laravel 5.4,所以今天我尝试在我的laravel项目中修改默认的auth 首先:Composer(1.4.2)和Laravel(5.4.27)(也意味着所有依赖项)是最新的。我通过以下方式验证了这一点: composer self-update composer update 然后,我通过迁移更改了users表: Schema::table('users', function (Blueprint $users){ $users->string('login', 16)->unique

所以今天我尝试在我的laravel项目中修改默认的auth

首先:Composer(1.4.2)和Laravel(5.4.27)(也意味着所有依赖项)是最新的。我通过以下方式验证了这一点:

composer self-update
composer update

然后,我通过迁移更改了users表:

Schema::table('users', function (Blueprint $users){
    $users->string('login', 16)->unique()->after('id');
    $users->string('real_name', 32)->after('email');
});

Schema::table('users', function(Blueprint $users){
    $users->dropColumn([
        'name'
    ]);
});
重要的一点是我想用“login”而不是“name”

我做的下一件事是修改用户类,如下所示:

protected $fillable = [
    'login',
    'email',
    'real_name',
    'password'
];
protected $primaryKey = 'login';

public function getAuthIdentifierName()
{
    return 'login';
}
还有LoginController:

public function username()
{
    return 'login';
}
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required|string',
        'password' => 'required|string',
        'g-recaptcha-response' => 'required|captcha'
    ]);
}
最后,我更改了登录视图:

<div class="form-group{{ $errors->has('login') ? ' has-error' : '' }}">
    <label for="login" class="col-md-4 control-label">Login</label>

    <div class="col-md-6">
        <input id="login" type="text" class="form-control" name="login" value="{{ old('login') }}" required autofocus>

        @if ($errors->has('login'))
            <span class="help-block">
                <strong>{{ $errors->first('login') }}</strong>
            </span>
        @endif
      </div>
</div>
正在正确加载除“登录”之外的所有数据。由于某些原因,此字段保持为“0”(在身份验证web中)

当最终显示默认主视图时,执行的查询是:

select * from `users` where `login` = '0' limit 1

我在Windows10上使用XAMPPV3.2.2、PHP7.1.1和10.2.6-MariaDB作为本地开发环境。我应该提到的是,我用最新稳定的MariaDB版本的新下载替换了这个XAMPP版本的默认MariaDB。(因为迁移的一个错误)


我还检查了StackOverflow、Laracasts等网站上关于auth的多篇文章,但没有找到任何对我有帮助的文章

e、 g.

回答OP


删除代码
protected$primaryKey='login'
getAuthIdentifierName()
方法

您只需在控制器中重写(添加)
username()
方法


有关迁移以及如何正确执行迁移的注意事项:

  • 始终使用迁移来创建和更新数据库架构
  • 如果您是一名单独的开发人员,并且没有设置生产环境,只需修改迁移并执行数据库重置(删除所有表)+迁移即可
  • 如果您是一个团队,或者已经设置了生产环境,请始终创建新的迁移
  • 不要为
    down()
    方法操心那么多

  • 大约30分钟后,可以在这个播客中听到来自Laravel的创建者和朋友关于迁移的一些材料。

    protected$primaryKey='login'删除它,您将主键作为id。。。
    getAuthIdentifierName()
    方法也是如此。迁移:如果您已经有一些用户使用重命名方法,如果您没有任何用户,并且您仍在开发中,请忘记新的迁移,只需更改默认迁移。这样做了,谢谢!)@Kyslik请留下您的评论作为回答!这样,更多的人可以用同样的问题更快地看到答案@Jsleshem我认为这个问题只是因为缺少文档记录时间。也许明天我会写一个扩展版本。谢谢你的指点。@Jsleshem你看:)
    "user" => array:10 [
        "id" => 1
        "email" => "email@email.com"
        "created_at" => "2017-06-16 03:08:43"
        "updated_at" => "2017-06-16 03:08:43"
        "login" => 0
        "real_name" => "PolluX"
    
    select * from `users` where `login` = '0' limit 1