[英]Laravel question: How can I change the email field to username?
I'm currently using the default Authentication of Laravel (php artisan make:auth)我目前使用的是默认身份验证 Laravel (php artisan make:auth)
But since it's very crucial for us to change the email-field to a username-field, I will need some help on that.但是由于将电子邮件字段更改为用户名字段对我们来说非常重要,因此我需要一些帮助。 I already tried to give it a shot, but I couldn't figure it out.
我已经试过了,但我想不通。
Help would be nice.帮助会很好。
As you can see in the authentication quickstart from the Laravel's Documentation at the "Username Customization" paragraph, you can easily customize the data that the user use to authenticate : 如您在Laravel文档的“用户名自定义”段落中的身份验证快速入门中所见,您可以轻松地自定义用户用于身份验证的数据:
By default, Laravel uses the email field for authentication.
默认情况下,Laravel使用电子邮件字段进行身份验证。 If you would like to customize this, you may define a username method on your LoginController :
如果您想对此进行自定义,则可以在LoginController上定义一个username方法:
public function username()
{
return 'username';
}
Beside @JeuneApprenti answer you also needs to make changes to RegisterController.php because email is declared required.除了@JeuneApprenti 的回答,您还需要对 RegisterController.php 进行更改,因为声明需要 email。 So you may need to change it and also insert and make username as required.
因此,您可能需要更改它并根据需要插入和创建用户名。 So in validator method of RegisterController
所以在 RegisterController 的验证器方法中
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string', 'max:255', 'unique:users'],
'email' => ['string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
and also the method create还有创建方法
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
Also you need to make changes to AuthenticatesUsers.php because LoginController uses it against validation.您还需要对 AuthenticatesUsers.php 进行更改,因为 LoginController 使用它来验证。
public function username()
{
return 'username';
}
First delete the users table.首先删除用户表。 and then execute migration again.
然后再次执行迁移。
DROP TABLE users;
Also delete the entry that user table migration is done.同时删除用户表迁移完成的条目。
DELETE FROM migrations where migration = '2014_10_12_000000_create_users_table';
Do make changes to database migrations file for users.一定要为用户更改数据库迁移文件。 In 2014_10_12_000000_create_users_table.php file in database/migrations folder.
在 database/migrations 文件夹中的 2014_10_12_000000_create_users_table.php 文件中。 Change the method up as
将方法更改为
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->enum('authority', ['admin','oper','guest']);
$table->rememberToken();
$table->timestamps();
});
Now execute the migration现在执行迁移
php artisan migrate
Plus now that you had declared username in database we also need to make changes in the Auth forms to insert the username filed in the html另外,既然您已经在数据库中声明了用户名,我们还需要在 Auth forms 中进行更改,以插入在 html 中提交的用户名
First make changes to resources/views/auth/register.blade.php form.首先更改 resources/views/auth/register.blade.php 表单。 Add the following code after email. (In case you didn't need email then replace the following code with email div block.
在 email 之后添加以下代码。(如果您不需要 email,则将以下代码替换为 email div 块。
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username">
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
You may need to rerun npm to recreate app.js您可能需要重新运行 npm 以重新创建 app.js
npm run dev npm 运行开发
Also make changes to login.blade.php form in /resources/views/auth folder.还要更改 /resources/views/auth 文件夹中的 login.blade.php 表单。
Replace the following替换以下内容
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
with和
<div class="form-group row">
<label for="username" class="col-md-4 col-form-label text-md-right">{{ __('User Name') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control @error('username') is-invalid @enderror" name="username" value="{{ old('username') }}" required autocomplete="username" autofocus>
@error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
Lastly make changes to app/User.php model.最后修改 app/User.php model。
Change fillable array to将可填充数组更改为
protected $fillable = [
'name', 'username', 'email', 'password',
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.