简体   繁体   English

Laravel 问题:如何将email字段更改为用户名?

[英]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.

相关问题 如何使用预定义的电子邮件/用户名和密码登录Laravel? - How Can I Login In Laravel With Predefined Email/Username and Password? 如何使用Laravel 5.0进行Restful Password Reminder并将User email字段更改为username字段? - How to make a Restful Password Reminder and change User email field to username field with Laravel 5.0? 如何更改LDAP中的用户名或电子邮件或电话号码信息? - How can I change username or email or phone number information in LDAP? Laravel 5.6将电子邮件字段名称更改为用户名以忘记密码 - Laravel 5.6 Change email field name as username for forgot password 更改为用户名而不是电子邮件Laravel - Change to Username instead of Email Laravel 如何更改LARAVEL 5上身份验证的默认用户名/电子邮件/密码? - How to change default username/email/password for Authentication on LARAVEL 5? Laravel:如何更改默认的身份验证密码字段名称? - Laravel: How can I change the default Auth Password field name? Laravel雄辩,如果“匿名”字段为1,则将“用户名”更改为匿名 - Laravel eloquent if 'anonym' field is 1, change 'username' to anonym 我想使用laravel中的用户名或电子邮件更新登录名 - I want to update login with username or email in laravel Symfony 4按用户名更改密码 - 电子邮件不能为空 - Symfony 4 change password by username - email can not be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM