Reputation: 2567
I have updated the laravel users table by adding few more field.usertype was one of it. Then I have updated the User model fillable field and also I have added required input types into my form. After all of these I have run php artisan migrate:refresh after that I can see that my table field also updated. But when I submit the form it generates the below error saying usertype dosent have a default value. usertype field is not a null filed .Can anyone say me whats wrong in this. For more references please refer below codes.
migration
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();
});
User Model
protected $fillable = [
'name',
'email',
'password',
'usertype',
];
User Registration form
@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>
Usercontroller
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.'));
}
This is the error it generates while I try to submit the form
SQLSTATE[HY000]: General error: 1364 Field 'usertype' doesn't have a default value (SQL: insert into
users
(name
,password
,updated_at
,created_at
) values (Nipun Tharuksha, [email protected], $2y$10$fHtc0yTpAFrGcO7dfg1qRe.EYZY/UNY4Ygn5qvUvcCSwoHjUjB6Gu, 2019-04-19 15:36:36, 2019-04-19 15:36:36))
Upvotes: 0
Views: 793
Reputation: 2567
Thanks for commenting on this. I was updating Controller UserController . But as @tharakadilshan mentioned I updated mt RegisterController and now its working. Once again thanks for all the comments. Thanks
Upvotes: 0
Reputation: 14268
As the error says you are not providing any value for the usertype column, and you are trying to save it with null
value. So in that case you can change the migration to allow null
by default like this:
$table->string('usertype')->nullable();
And once you change the migration you will have to re-run it using:
php artisan migrate:fresh
Warning: Keep in mind that this will destroy your current data.
Upvotes: 1