我安装了jeroennoten / Laravel-AdminLTE 这是软件包版本2.x(Laravel 6)。安装后,我修改了用户表,register.blade.php和registercontroller以获取用户名和角色。但是,当我提交表单时,它总是给我一个错误,未为用户名设置默认值。 这是我的观点
<div class="form-group has-feedback {{ $errors->has('role') ? 'has-error' : '' }}">
<select name="role" class="form-control">
<option>Register As</option>
<option value="candidate">Candidate</option>
<option value="voter">Voter</option>
</select>
<span class="glyphicon glyphicon-role form-control-feedback"></span>
@if ($errors->has('role'))
<span class="help-block">
<strong>{{ $errors->first('role') }}</strong>
</span>
@endif
</div>
<div class="form-group has-feedback {{ $errors->has('username') ? 'has-error' : '' }}">
<input type="text" name="username" class="form-control" value="{{ old('username') }}"
>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
还有我的registercontroller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'username' => ['required', 'string'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
'role' => ['required'],
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role' => $data['role'],
]);
}
在我提交后进行修改后,我总是会收到此错误
SQLSTATE [HY000]:常规错误:1364字段“用户名”没有 默认值(SQL:插入
users
(name
,password
,updated_at
,created_at
)值(Tanvir,tahmedhera @ gmail.com, $ 2y $ 10 $ 5XxgDoe272G0wkrcpQYmLepTCGPTPzASBkzXxJ84EhZqOEBvy4zFS, 2019-10-25 10:54:25,2019-10-25 10:54:25))请有人帮忙 我为什么会收到这个错误
答案 0 :(得分:0)
检查您的模型,我认为缺少用户名参数。
protected $fillable = [
'name', 'email', 'password', 'username', 'role'
];
答案 1 :(得分:0)
在模型User.php
中,您需要在username
中添加$fillable
您的模型User.php
应该类似于:
protected $fillable = [
'name', 'email', 'password', 'role', 'username
];
谢谢。