Customize Laravel Registration Form With Additional Fields

Aman Dhiman
Jun 1 · 4 min read

Hey Guys! This is Aman. Hope you all are doing well. Hope you all read my last blog which was of the Laravel Authorization. In that blog, we learned how to work with Laravel Auth package and how to make Login, Registration, Forgot Password, etc. Now, Let’s continue with the Laravel Auth and proceed further with the registration form customization with additional fields.

As you have seen the default registration form Laravel provides us in my last blog. It just has Name, E-Mail Address, Password and Confirm Password.

Sometimes we required some other fields in the form like Phone Number, Address, Gender, Date of Birth and so on.

Let’s start the development with the registration form customization with a new field. I am going to create Phone Number.

Step 1: First make a migration to create a new field in the database table. Command will be like.

php artisan make:migration add_phone_field_to_users_table --table=users

Now, you can see in the database->migrations we have a newly migrated migration file.

Step 2: Open the file in the editor and modify the up() and down() functions. It will look like this.

Step 3: After that, run the migration by following the command.

php artisan migrate

This migration will create a new field in the database.

Step 4: Go to resources/auth/register.blade.php and edit the registration form by adding the below HTML code.

<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 @error('phone') is-invalid @enderror" name="phone" required >@error('phone')<span class="invalid-feedback" role="alert"><strong>{{ $message }}</strong></span>@enderror</div></div>

Tada!! We can see a new field in the registration form.

Step 5: Oh my God! Another step 🙄. Yes, it is required. Because it will save the phone number in the database. Don’t worry it is not that difficult 😁

Open RegisterController.php controller found in app/Http/Controllers/Auth, and modify the validator. It will look like this.

protected function validator(array $data){return Validator::make($data, ['name' => ['required', 'string', 'max:255'],'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],'password' => ['required', 'string', 'min:8', 'confirmed'],'phone' => ['required', 'numeric', 'min:11']]);}

After this, scroll down in the same file and modify the create function. It will look like

protected function create(array $data){return User::create(['name' => $data['name'],'email' => $data['email'],'password' => Hash::make($data['password']),'phone' => $data['phone']]);}

Step 6: This is the final step. Save the RegisterController.php file open app/User.php and add ‘phone’ field in the $fillable property and save the file.

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

That’s it. Now, test the registration form you can see the saved number field in the database.

Yeah!!! It is done. Well done 👍 Now, enjoy the work you have done and follow the same steps if you need more fields in the form.

Try these steps and make the Customization working in your Laravel application form 😎👍

I created one sample Laravel application. You can visit my repo and fork it or clone the code from my repo and start work further. Click here to visit my GitHub repo.

This is all about . If you found this article useful please clap 👏 for this article.

Have a happy coding! 🤗

Aman Dhiman

Written by

I am PHP developer. Currently, I am working with ZestGeek Solutions. I just started write blogs on Medium.