I am currently working on a Laravel project and I need to save the content of the data received during user registration in a table called users.i was able to save other values correctly in the table except the value of the html select options.
Below is my html form:
<div class="form-group">
<label for="User_type" class="col-md-4 col-form-label text-md-right">{{ __('Select User type'
</label>
<div class="col-md-6">
<select class="form-input" name="signup_type">
<option value="single_user">single_user</option>
<option value="multi_user">multi_user</option>
</select>
</div>
</div>
and here is my laravel controller to add the needed values to database:
protected function validator(array $data)
{
return Validator::make(
$data, ['user_name' => ['required', 'string', 'max:32', 'unique:users'],
'name' => ['required', 'string', 'max:100'],
'mobile_phone' => ['required', 'string', 'max:15', 'unique:users'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'signup_type' => ['required','string','max:30'],]
);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
if (substr($data['mobile_phone'], 0, 1) == 0) {
$data['mobile_phone'] = substr(
$data['mobile_phone'], 1, strlen($data['mobile_phone'])-1
);
}
$data['mobile_phone'] = $data['dialingcode'].$data['mobile_phone'];
return User::create(
['user_name' => $data['user_name'],
'name' => $data['name'],
'email' => $data['email'],
'mobile_phone' => $data['mobile_phone'],
'password' => Hash::make($data['password']),
'signup_type' => $data['signup_type'],
'role_id' => 1,]
);
}
but each time I check the value of signup_type in database it is always null.
您应该在User
模型中检查fillable
和受guarded
数组,以便在该字段上也允许批量分配。
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.