ADDING RADIO BUTTON,SELECT, CHECKBOX IN REGISTRATION FIELDS: LARAVEL
Step:1 Add view for the new fields in register.blade.php
Nb: html tags for div must be put correctly, removed here to display code
Nb: html tags for div must be put correctly, removed here to display code
<!--number --> div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}" <label for="age" class="col-md-4 control-label">Age</label> div class="col-md-6"> <input id="age" type="number" min="1" max="120" class="form-control" name="age" value="{{ old('age') }}" autofocus> @if ($errors->has('age')) <span class="help-block"> <strong>{{ $errors->first('age') }}</strong> </span> @endif /div> /div> <!--radiobutton --> 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> <!--select--> <div id="edu-group" class="form-group{{ $errors->has('edudetails') ? ' has-error' : '' }}"> <label for="edudetails" class="col-md-4 control-label">Education and Occupation</label> div class="col-md-6"> Highest-Education <select name="education"> <option value="Graduate">Graduate</option> <option value="Plustwo">Plustwo</option> <option value="HighSchool">HighSchool</option> <option value="UpperPrimary">UpperPrimary</option> <option value="Primary">Primary</option> </select> <br></br> Occupation <select name="occupation"> <option value="Student">Student</option> <option value="Private">Private</option> <option value="Government">Government</option> <option value="Organisation">Organisation</option> <option value="Freelancer">Freelancer</option> </select> @if ($errors->has('edudetails')) <span class="help-block"> <strong>{{ $errors->first('edudetails') }}</strong> </span> @endif /div> /div> <!--checkbox--> div class="form-group{{ $errors->has('sports') ? ' has-error' : '' }}"> <label for="sports" class="col-md-4 control-label">Sports</label> div class="col-md-6"> <input type="checkbox" name="check[]" value="Football"/> Football <input type="checkbox" name="check[]" value="Cricket"/> Cricket <input type="checkbox" name="check[]" value="Tennis"/> Tennis @if ($errors->has('sports')) <span class="help-block"> <strong>{{ $errors->first('sports') }}</strong> </span> @endif /div> /div>
Step:2 create new migration for new fields- gender,edudetails, sports
$ php artisan make:migration add_custom_field_to_users_table
Then open and edit to add the following in the newly created migrations from database/migrations/
public function up() { Schema::table('users', function(Blueprint $table){ $table->string('gender')->after('age'); $table->string('edudetails')->after('gender'); $table->string('sports')->after('edudetails'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function(Blueprint $table){ $table->dropColumn('gender'); $table->dropColumn('edudetails'); $table->dropColumn('sports'); }); }
Step3: run migration to create a table in the database
php artisan migrate
Step:3 ADD VALIDATION AND CREATE THE FIELD IN registerController.php from app/Http/controllers/auth
protected function create(array $data) { return User::create([ 'name' => $data['name'], 'age' => $data['age'], 'gender'=> $data['gender'], 'edudetails'=> $data['education']." , " .$data['occupation'], 'sports'=> implode(',',$data['check']), 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); }
AS the $data[‘check’] returns an array, theimplode() function is used to convert an array to string and every checked value will now be available in the database.
STEP 4: Update user.php from app/user.php
protected $fillable = [ 'name','age','gender','sports','edudetails', 'email', 'password', ];
Now try registering and check the database
Post a Comment