在数据库laravel中保存下拉列表值

时间:2017-09-12 19:36:20

标签: laravel validation drop-down-menu

我试图通过下拉列表在注册表中询问用户的性别,但我无法在控制器中获得该值。

这是我的注册码.blade.php

<!-- Gender -->

<div class="form-group">
    <label for="gender" class="col-md-4 control-label">Gender</label>

    <div class="col-md-6">
         <select class="form-control" required="required">
            <option value="male">Male</option>
            <option value="female">Female</option>
        </select>
    </div>
</div>  

代码register controller

protected function validator(array $data)
{
    return Validator::make($data, [
        'username' => 'required|string|max:255',
        'fullname' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'dob' => 'required|date',
        'gender' => 'required|string',
        // 'gender' => 'in:male,female'

    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    dd($data);
    return User::create([
        'username' => $data['username'],
        'fullname' => $data['fullname'],

        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'dob' => $data['dob'],
         'gender' => $data['gender'],

    ]);
}

user model包含

protected $fillable = [
    'username', 'email', 'password','fullname','dob','gender'
];

和行&#39;性别&#39;时显示的输出=&GT; &#39; required | string&#39;,在寄存器控制器的验证器功能中注释。取消注释此行时没有任何反应 enter image description here

2 个答案:

答案 0 :(得分:0)

错过了字段名称。

<select class="form-control" name="gender" required="required">
    <option value="male">Male</option>
    <option value="female">Female</option>
</select>

答案 1 :(得分:0)

name="gender"元素中写入属性select

如果没有name属性,则不会通过GET / POST请求传递数据。