Laravel 7: Add a New username Field to the Default Authentication
First of all, you need to add the username
field in the users table. To do that, add the following in create_users_table.php
$table->string('username');
Now, migrate it.
php artisan migrate:refresh
Add it in the User.php
model in the protected field:
protected $fillable = [
'name', 'email', 'password', 'username',
];
Now, we have to add it in the RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'username' => ['required','string','max:255','unique:users','alpha_dash'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'username' => $data['username'],
}
Add it in the view:
<input type="text" name="username" placeholder="Username">
Now, you should be good to go!