January 15, 2018
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.
1 2 3 4 |
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
1 |
$ 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:
1 2 3 4 5 6 7 8 9 10 11 |
/** * Run the migrations. * * @return void */ public function up() { Schema::table('users', function (Blueprint $table) { $table->string('test_field'); }); } |
down() method:
1 2 3 4 5 6 7 8 9 10 11 |
/** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('test_field'); }); } |
3. Run migration
1 |
$ 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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/** * 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * 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)
1 2 3 4 5 6 7 8 |
/** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'phone', 'test_field', 'password', ] |
4 Comments
Thanks for this great article. I have to confess as a beginner Laravel developer, this is one of the basic stuffs have been struggling with. Thanks to this your write-up.
but its not working for me.they say
“trying to get proprty “headers” of non-object.why is that?
I am 43 year old female. I just bookmarked your page
Hi there! Someone in my Facebook group shared this site with us so I came to take a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers! Superb blog and wonderful style and design.