I have created a registration form using Laravel 6 default migration table and Auth register controller. Now I want to have two types of accounts, so I have a added a checkbox to the HTML form. If the checkbox is checked, the account should be of different type than when the checkbox in unchecked.
What I have:
Database tables:
account_types (id, name)
users (id, name, email, password, account_type_id)
HTML
<label class="switch">
<input id="register-toggle-switch" name="account-type" type="checkbox" autocomplete="off" checked onclick="toggleswitch()">
</label>
RegisterController.php
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
// 'account_type_id' => ['required']
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
if (isset($_POST['account-type']))
{
$account = '1';
}
else if(!isset($_POST['account-type']))
{
$account = '2';
}
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'account_type_id' => $account
]);
}
However this does not work as it returns an error saying: "General error: 1364 Field 'account_type_id' doesn't have a default value" and then proceeds to show the attempted query which does not include the 'account_type_id' or its value at all.
So the question is: Depending of the state of the checkbox in the form, how can I pass the respective id into the registration query ?