我目前正在使用laravel Auth,我想在注册页面中添加性别选择字段,但它会返回错误。
未定义的索引:性别
这是我在刀片中的形式部分:
<div class="form-group{{ $errors->has('gender') ? ' has-error' : '' }}">
<label for="gender" class="col-md-4 control-label">Gender</label>
<div class="col-md-6">
<select class="form-control">
<option value="0" name="gender">Male</option>
<option value="1" name="gender">Female</option>
</select>
@if ($errors->has('gender'))
<span class="help-block">
<strong>{{ $errors->first('gender') }}</strong>
</span>
@endif
</div>
</div>
我的User
型号:
protected $fillable = [
'name', 'email', 'password', 'facebook_id', 'gender', 'avatar', 'username'
];
RegisterController
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/admin/dashboard';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* 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',
'username' => 'required|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'gender' => 'sometimes|string',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'gender' => $data['gender'],
'password' => bcrypt($data['password']),
]);
}
}
这是我的迁移:
$table->string('gender')->nullable();
知道它为什么不保存到数据库?
答案 0 :(得分:2)
您需要将名称应用于选择,而不是选项。像这样改变
<select class="form-control" name="gender">
<option value="0">Male</option>
<option value="1">Female</option>
</select>