繁体   English   中英

使用 Laravel 控制器在数据库中保存 html 选择选项的值

[英]Saving the value of html select option in database using Laravel controller

我目前正在处理一个 Laravel 项目,我需要将用户注册期间收到的数据内容保存在一个名为 users.i 的表中,除了 html 选择选项的值之外,我能够在表中正确保存其他值。

下面是我的 html 表单:

<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>

这是我的 Laravel 控制器,用于将所需的值添加到数据库中:

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,]
      );  
}

但是每次我检查数据库中 signup_type 的值时,它总是为空。

您应该在User模型中检查fillable和受guarded数组,以便在该字段上也允许批量分配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM