Laravel 5.4,重命名用户表列

问题描述:

所以今天我试图在laravel项目中修改默认身份验证.

So today I tried to modify the default auth in my laravel project.

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

First of all: Composer (1.4.2) and Laravel (5.4.27) (meaning also all dependencies) are up to date. I verified this with:

composer self-update
composer update


然后我通过迁移更改了用户表:


Then I altered the users table via a migration:

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'
    ]);
});

重要的是我要使用登录"而不是名称".

The important part of that is that I want to use 'login' instead of 'name'.

接下来我要做的就是修改User类,如下所示:

Next thing I did was modifying the User class as follows:

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

public function getAuthIdentifierName()
{
    return 'login';
}

还有LoginController:

And also the 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'
    ]);
}

最后,我更改了登录视图:

Finally I changed the login view:

<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>


现在我应该具有有效的身份验证了,对吗?这就是我的想法.


Now I should have a working authentication, right? That's what I thought.

登录本身可以工作,但是在读取登录"字段的值时会失败-或类似的事情.

The login itself works but when it comes to reading the value of the field 'login' it fails - or something like this.

调试栏告诉我以下内容:

debug-bar told me the following:

将在登录时执行的查询为:

The query which will be executed on login is:

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

除了登录"之外,哪个可以正确加载所有数据.由于某些原因,该字段保持为"0"(在身份验证网络中)

Which is loading all the data correctly except of 'login'. For some reason this field stays '0' (in auth web)

"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"

最后要显示默认主视图时,执行的查询为:

And when it finally comes to displaying the default home view the executed query is:

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


我正在使用XAMPP v3.2.2,PHP 7.1.1和10.2.6-MariaDB作为Windows 10上的本地开发环境.我应该提一下,我用全新下载的XAMPP版本替换了该XAMPP版本的默认出厂MariaDB.最新的稳定MariaDB版本. (由于迁移的错误)


I'm using XAMPP v3.2.2, PHP 7.1.1 and 10.2.6-MariaDB as my local dev environment on Windows 10. I should mention that I replaced the default shipped MariaDB of this XAMPP version with a fresh download of the latest stable MariaDB version. (because of an error of the migration thing)

我还检查了关于StackOverflow,Laracasts等上的auth内容的多个帖子,但没有找到任何可以帮助我的帖子.

I also checked multiple posts on the auth thing on StackOverflow, Laracasts and so on but didn't find any post that could've helped me.

例如 https://laracasts.com/discuss/channel/laravel/更改名称列到身份验证中的用户名

OP的答案.

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

您要做的就是在控制器中重写(添加)username()方法.

All you have to do is override (add) username() method in the controller.

有关迁移及其正确操作的说明:

Note for migrations and how to do it right:

  1. 始终使用迁移来创建和更新数据库架构
  2. 如果您是唯一的开发人员 ,并且没有设置没有生产环境,只需修改迁移并重置数据库即可(删除所有表格)+迁移
  3. 如果您是团队已经建立了生产环境,请始终创建新的迁移
  4. down()方法不那么在意
  1. always use migrations to create and update database schema
  2. if you are a lone developer AND there is no production environment set up, just amend the migrations and do database reset (remove all tables) + migrate
  3. if you are a team OR there is production environment already set up, always create new migration
  4. do not bother with down() method that much

创作者&的一些材料您可以在此播客 http://www上收听Laravel的有关迁移的朋友.laravelpodcast.com/episodes/68236-episode-53-bigger-better 大约30分钟.

Some material from creator & friends of Laravel regarding migrations can be heard in this podcast http://www.laravelpodcast.com/episodes/68236-episode-53-bigger-better around 30 minute mark.