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 ie 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, abc@gmail.com, $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 ie 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.
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'],
]);
}
}
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.
/**
* 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();
});
}
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*/
];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.