Powered by Blogger.

LARAVEL: ADDING CUSTOM FIELD BY CREATING NEW MIGRATION WITH COLUMN FIELDS


I’ll walk you through an example in which we require the user to additionally provide the gender field.
Assuming that you have already registered users and checked the database with make: auth and migrate method
STEP:1 MAKING NEW MIGRATION WITH NEW FIELD TO TABLE
Begin by updating the users table to include a gender a field without affecting already registered values in the database:
$ php artisan make:migration add_gender_field_to_users_table
Open the newly created migration file from [database/migrations/]and modify the up method to look like this:
Schema::table('users', function(Blueprint $table)
 {
 $table->string('gender');
 });
Modify the down method to look like this:
Schema::table('users', function(Blueprint $table)
 {
 $table->dropColumn('gender');
 });
Run the migration
php artisan migrate
Now refresh the user's table in the database. You can find a new column added to the end of the user's table.
if you wish to place a new field next to a particular column you have to specify with->after(‘column’) inside schema.
Now you need to update migration by first rolling back to –step=1, inorder to delete the earlier created column at the last of the user's table and then run migrate
$ php artisan migrate:rollback --step=1
Then add
Schema::table('users', function(Blueprint $table) {
 $table->string('gender')->after('age');
 });
after that run migration
$ php artisan migrate
Now you have a gender column next to age without compromising already registered members data.
and then open the resources/auth/register.blade.php
STEP:2 ADDING FIELD IN VIEW PAGE
edit register.blade.php from resources/views/auth
nb: put appropriate html tags for div, here purposefully removed to display it
div id="gender-group" class="form-group{{ $errors->has('gender') ? ' has-error' : '' }}"
 <label for="gender" class="col-md-4 control-label">Gender</label>

div class="col-md-6"
 div<input id="female" type="radio"class="form-control" name="gender" value="Female" {{ (old('sex') == 'female') ? 'checked' : '' }} >Female/div
 div<input id="male" type="radio"class="form-control" name="gender" value="Male" {{ (old('sex') == 'male') ? 'checked' : '' }} >Male/div
 div<input id="other" type="radio"class="form-control" name="gender" value="Others" {{ (old('sex') == 'other') ? 'checked' : '' }} >Other/div
 @if ($errors->has('gender'))
 <span class="help-block">
 <strong>{{ $errors->first('gender') }}</strong>
 </span>
 @endif
 /div
 /div
STEP 3: ADD VALIDATION AND CREATE THE FIELD IN registerController.php from app/Http/controllers/auth
protected function validator(array $data)
 {
 return Validator::make($data, [
 'name' => 'required|string|max:255',
 'age' => 'integer|max:20',
 'gender'=> 'required|string|max:255',
 'email' => 'required|string|email|max:255|unique:users',
 'password' => 'required|string|min:6|confirmed',
 ]);

........
  protected function create(array $data)
 {
 return User::create([
 'name' => $data['name'],
 'age' => $data['age'],
 'gender'=> $data['gender'],
 'email' => $data['email'],
 'password' => bcrypt($data['password']),
 ]);
 }
STEP 4: Update user.php from app/user.php
 protected $fillable = [
 'name','age','gender', 'email', 'password',
 ];
Now try to register and check the database to see the result

No comments