我通过添加更多字段来更新了 laravel用户表。用户类型就是其中之一。然后,我更新了用户模型可填写字段,并且还向表单中添加了必需的输入类型。完成所有这些操作后,我运行了 php artisan migrate:refresh 之后,我可以看到我的表字段也已更新。但是,当我提交表单时,它会产生以下错误,提示用户类型剂量具有默认值。 用户类型字段不是无效。任何人都可以说我这有什么问题。有关更多参考,请参考以下代码。
迁移
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
//added by myself
$table->string('usertype');
$table->boolean('activestatus')->default(false);
//
$table->rememberToken();
$table->timestamps();
});
用户模型
protected $fillable = [
'name',
'email',
'password',
'usertype',
];
用户注册表格
@csrf
<input type="text" name="usertype" id="hiddenUserType" value="a" hidden/>
<div class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-hat-3"></i></span>
</div>
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" placeholder="{{ __('Name') }}" type="text" name="name" value="{{ old('name') }}" required autofocus>
</div>
@if ($errors->has('name'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" placeholder="{{ __('Email') }}" type="email" name="email" value="{{ old('email') }}" required>
</div>
@if ($errors->has('email'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('password') ? ' has-danger' : '' }}">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" placeholder="{{ __('Password') }}" type="password" name="password" required>
</div>
@if ($errors->has('password'))
<span class="invalid-feedback" style="display: block;" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
<div class="form-group">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control" placeholder="{{ __('Confirm Password') }}" type="password" name="password_confirmation" required>
</div>
</div>
{{-- <div class="text-muted font-italic">
<small>{{ __('password strength') }}: <span class="text-success font-weight-700">{{ __('strong') }}strong</span></small>
</div> --}}
<div class="row my-4">
<div class="col-12">
<div class="custom-control custom-control-alternative custom-checkbox">
<input class="custom-control-input" id="customCheckRegister" type="checkbox">
<label class="custom-control-label" for="customCheckRegister">
<span class="text-muted">{{ __('I agree with the') }} <a href="#!">{{ __('Privacy Policy') }}</a></span>
</label>
</div>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary mt-4">{{ __('Create account') }}</button>
</div>
</form>
用户控制器
public function store(UserRequest $request, User $model)
{
$model->create($request->merge([
'password' => Hash::make($request->get('password'))
])->all());
return redirect()->route('user.index')
->withStatus(__('User successfully created.'));
}
这是我尝试提交表单时生成的错误
SQLSTATE [HY000]:常规错误:1364字段“用户类型”没有 默认值(SQL:插入
users
(name
,password
,updated_at
,created_at
)值(Nipun Tharuksha,nipun @ gmail.com, $ 2y $ 10 $ fHtc0yTpAFrGcO7dfg1qRe.EYZY / UNY4Ygn5qvUvcCSwoHjUjB6Gu, 2019-04-19 15:36:36,2019-04-19 15:36:36))
答案 0 :(得分:0)
如错误所述,您没有为usertype列提供任何值,而是尝试使用null
值保存它。因此,在这种情况下,您可以将迁移更改为默认允许null
,如下所示:
$table->string('usertype')->nullable();
更改迁移后,您将必须使用以下命令重新运行它:
php artisan migrate:fresh
警告:请记住,这会破坏您当前的数据。
答案 1 :(得分:0)
感谢对此发表评论。我正在更新Controller UserController。但是正如@tharakadilshan所提到的,我更新了mt RegisterController并更新了它的工作方式。再次感谢您的所有评论。 谢谢