Skip to content
Advertisement

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

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 AppUser
     */
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.

Advertisement

Answer

You should check in the User model the fillable and the guarded arrays, in order to allow mass assignment also on that field.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement