Php Laravel 5.4,重命名用户表列
所以今天我尝试在我的laravel项目中修改默认的auth 首先:Composer(1.4.2)和Laravel(5.4.27)(也意味着所有依赖项)是最新的。我通过以下方式验证了这一点: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
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