When registering a new user, I want them to pick a unique username. Everything worked with the username when I used Jetstream but when I rebuilt with Laravel Fortify and Laravel UI, the username is null regardless of what the user enters in the field. Below I have included several samples of code used to add and register the username.
I am not getting any errors in debugging or in the logs to support any known issues.
Register.blade.php (Username Input)
<div class="px-2"> <x-general.form.label for="username" label="{{ __('Username') }}" class="" /> <x-general.form.input type="text" name="username" class="@error('username') is-invalid @enderror" value="" /> @error('username') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div>
AppActionsFortifyCreateNewUser.php (create function)
public function create(array $input) { Validator::make($input, [ 'name' => ['required', 'string', 'max:255'], 'username' => ['required', 'string', 'max:16'], 'email' => [ 'required', 'string', 'email', 'max:255', Rule::unique(User::class), ], 'password' => $this->passwordRules(), ])->validate(); return User::create([ 'name' => $input['name'], 'username' => $input['username'], 'email' => $input['email'], 'password' => Hash::make($input['password']), ]); }
User Model
<?php namespace AppModels; use IlluminateContractsAuthMustVerifyEmail; use IlluminateDatabaseEloquentFactoriesHasFactory; use IlluminateFoundationAuthUser as Authenticatable; use IlluminateNotificationsNotifiable; class User extends Authenticatable { use HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'username', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; }
Create Users Table Migration
public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->string('username')->unique(); $table->string('avatar')->nullable(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
UserFactory
<?php namespace DatabaseFactories; use AppModelsUser; use IlluminateDatabaseEloquentFactoriesFactory; use IlluminateSupportStr; class UserFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = User::class; /** * Define the model's default state. * * @return array */ public function definition() { return [ 'name' => $this->faker->name, 'username' => $this->faker->unique()->userName, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; print_r($this->faker->unique()->userName); exit; } }
I can’t figure out why the username isn’t being submitted. This is all the same code as I used in my Jetstream based project.
Any help is wonderful and appreciated!
Advertisement
Answer
You need to do $this->faker->unique()->userName,
you are missing capitalization there on userName
.
See both:
https://laravel.com/docs/8.x/database-testing#creating-models https://github.com/fzaninotto/Faker#fakerprovideren_usperson
extract: userName // 'wade55'