How to add custom fields to registration form in Laravel

Last Updated: December 22, 2017

In this tutorial, you can study how to add custom field to Laravel. First you have to install Laravel in your computer and then install the Auth

You can follow the steps given below to add custom field to registration post

Create new Laravel project

Download the Laravel via composer

composer.phar global require "laravel/installer"

Create new Laravel project named custom

php composer.phar create-project --prefer-dist laravel/laravel oauth

Generate the Authentication scaffolding which has the all authentication related controllers, models and view. This will create table users in the database to hold user information

php artisan make:Auth

When you install the Laravel and Auth scaffolding you can start the web server by executing the following artisan command

 php artisan serve

This will start the Webserver and you can see the app at http://127.0.0.1:8000

Add Custom field to register page

Register page is located at resources\views\auth\register.blade.php. I am going to add filed to enter Phone

You can add the following code below the Email field

   
@if ($errors->has('phone')) {{ $errors->first('phone') }} @endif

Now you can see the register page at http://127.0.0.1:8000/register

Add field to table

All the user information is stored in the users table in the database so we need to add phone field to users table

I will create migration file to add field to users table

php artisan make:migration add_phone_field_to_users_table

I will add the following code to the database migration file created

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function(Blueprint $table)
        {
            $table->dropColumn('phone');
        });
    }

Next you can run the migrate command to add field to the table

 Niro$ php artisan migrate

Add code to RegisterController

Finally you can add the code to create method of the RegisterController located at App\Http\Controllers\Auth

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