2

I'm trying to create registration form in laravel but with my custom input fields, I've changed the 'register.bladed.php' to change the 'view'.

The default registration form was containing name , email , password, confirm Password.

I added two new input fields i.e gender and date-of-birth.

I made changes to the create_users_table.php with above two new input fields and then migrate that which results into the following table structure.

here it is

Now when I try to register any user it gives me the following error

SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn't have a default value (SQL: insert into users (name, email, password, updated_at, created_at) values (abc, [email protected], $2y$10$wGLT0qsJ3EmSSa4vzTj0sOop6eRvtPShg9.aEZ6wJFJY8OdHMsrBO, 2019-06-07 15:55:08, 2019-06-07 15:55:08))

I think I want to change something where the form is submitting, but there are two functions where the form is submitting i.e route register

1.validator 2. create

I don't know what to do with that ?

What changes do I need to make to have the new input new input fields working, and in future which things should I do to make custom input fields working correctly .

The name for date-of-birthis dob given as:

                        <div class="col-md-6">
                            <input id="dob" type="date" class="form-control @error('email') is-invalid @enderror" name="dob" value="{{ old('dob') }}" required autocomplete="dob">

                            @error('dob')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>

for gender it is :

                            <div class="col-md-6">
                            <select id="" class=" form-control" name = 'gender' required>
                                <option value="Male">Male</option>
                                <option value="Fe Male">Fe Male</option>
                            </select>

                            @error('gender')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>

I've used the same names in RegisterController.php.

the table looks like this :

   public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email');
        $table->date('dd/mm/yy')
        $table->string('gender');
        $table->timestamps();
    });
}

I've made some manual changes to the table in my phpmyadmin according to the above structure of the table, without php artisan migrate.

May be something that is obvious I'm asking about , but I'm new totally new to laravel.

Thank you so much.

4 Answers 4

3

you should add the two fields within the 2 methods validator and create of RegisterController like below :

ps: please check the fields name within your form

class RegisterController extends Controller
{

  // some code ... 


 protected function validator(array $data)
    {
        return Validator::make($data, [
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',

            'gender' => 'required',
            'date_of_birth' => 'required'
        ]);
    }


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

                'gender' => $data['gender'],
                'date_of_birth' =>  $data['date_of_birth'],
            ]);
        }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I made these changes, but it's giving the same error again
do you get checked the name of your new inputs as mine here within the tow methods?
Yes, and I've used name = 'gender' on select element, hope that is not a problem, just confirming .
update your question and put your html form code please
1

If you can afford it, delete the old table and create a new one. Adapts the class for migrations. This way SQL will know what to allocate by default if it is not informed. Try this.

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('gender')->default('Your default value');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

4 Comments

let me try that
should I store the date-of-birth as string or as date, because I don't know either date() function either exists or not ?
Me I use date() like this : $table->date('birth_date')->nullable();
Yes , you can use date() function like I've used , it will work perfectly fine.
1

In your table the field "gender" is non-null, which means that when registering a new user you must give it a value. Which is not done, which is why you have the mistake. You can still set a default value in your Migration class or specify Null: YES as for "email_verified_at for example for "gender" I hope that my explanations can help you.

2 Comments

But I'm giving a value to gender, but the query is not taking that , as I mentioned there is something wrong when the form is submitting and the gender is not being set for some reason
@mehmood did you not see my answer?
1

You have to edit the user model and add the name of the database column that corresponds to your custom fields in the fillable variable as follows:

protected $fillable = [
        'name', 'email', 'password',
        /* Custom Fields*/'gender_id', 'city_id',/* Custom Fields*/
    ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.