Laravel 8 Project Home Services - Add User Option and Phone On Register Page

In this video, we will learn about adding a user option and phone number on the register page.

Let's see how we can add a user option and phone number on the register page.

First of all, let's create a migration for adding a new column to the user model.

So, switch to the command prompt and type the command:


php artisan make:migration add_phone_to_users_table --table=users

Now, switch to the project and let's open the migration. Go inside the database directory, then migration. Now, from here, open the add_phone_to_users_table migration.

Add a new column:


public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phone')->nullable();
});
}

Now, save this and let's migrate this migration. So, switch to the command prompt and type the command:


Php artisan migrate

Now, let's run the application.

Switch to the project and let's open the register.blade.php file. Go inside the resources directory, then view auth, and from here, let's open the register.blade.php file.

Now, here, let's add a text field for the phone and a select control for registering a user as:


<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">Phone</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control" name="phone" value="" required="">
</div>
</div>

<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">Register As</label>
<div class="col-md-6">
<select class="form-control" name="registeras" id="registeras">
<option value="CST">Customer</option>
<option value="SVP">Service Provider</option>
</select>
</div>
</div>

Now, let's open the User model. Go inside the app directory, then models, and from here, let's open the User.php file. Now, inside the fillable, add here utype and phone:


protected $fillable = [
'name',
'email',
'password',
'phone',
'utype'
];

Now, go inside the app->Action->Fortify and from here, let's open the CreateNewUser.php file and modify the create method:


public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'phone' => ['required'],
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
])->validate();

$registeras = $input['registeras'] === 'SVP' ? 'SVP':'CST';

return User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
'phone' => $input['phone'],
'utype'=>$registeras
]);
}

Now, all done. So, let's check it.

Switch to the browser and refresh the page. Now, register a new user.

So, enter the name here, email, password, phone, and select user type as a service provider.

Now, click on submit. You can see the user registered successfully.

Now, let's register another user as a customer.

So, enter another name and all details, and select customer.

Now, click on submit. You can see this time the user created as a customer.

So, in this way, you can add a user option and phone number on the register page.