How to add extra fields to a laravel 5 registration form

I have added extra field to registration form and validated that field.But I had issue while making migration after adding extra column though migration file to save extra field value in database.When I migrate it won’t do any thing.After this, I run migration with refresh option but it roll backed all tables data.And after exploring more I have found better solution to add extra column through following code.

Schema::table('users', function($table)
{
$table->string('test_field');
});

 

Here you can achieve it through following steps.

1. Create new migration file using below command

$ php artisan make:migration add_extra_field_to_users_table --table=users

 

2. Open newly created  migration file and add extra field column through up() and down() method

up() method:

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('test_field');
});
}

down() method:

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

 

3. Run migration

$ php artisan migrate

After it a new column will be added in users table

5. To add extra form field, open \resources\views\auth\register.blade.php view file and add your form html

<div class="form-group{{ $errors->has('test_field') ? ' has-error' : '' }}">
<label for="test_field" class="col-md-4 control-label">Phone Number</label>

<div class="col-md-6">
<input id="test_field" type="text" class="form-control" name="test_field" value="{{ old('test_field') }}" required autofocus>

@if ($errors->has('test_field'))
<span class="help-block">
<strong>{{ $errors->first('test_field') }}</strong>
</span>
@endif
</div>
</div>

 

6. Validate extra field through auth controller RegisterController.php(\app\Http\Controllers\Auth)

 

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'phone' => 'required|numeric|digits_between:10,13',
'test_field' => 'required|string|max:255',
'password' => 'required|string|min:6|confirmed',
]);
}

 

7. Save extra field value to database through RegisterController.php(\app\Http\Controllers\Auth)

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{

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

Also update the extra field to User.php model(\app\User.php)

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'phone', 'test_field', 'password',
]

 

4 Comments

Add a Comment

Your email address will not be published. Required fields are marked *