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.
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
,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-birth
is 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
you should add the two fields within the 2 methods
validator
andcreate
ofRegisterController
like below :ps: please check the fields name within your form
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.
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.
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: