0

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, email, 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))

DB

2
  • first make sure that this UserController store() funciton is called. because I think laravel still calles for the original RegisterController create() function. thats why your custom fields are not included in sql query. Commented Apr 19, 2019 at 16:20
  • @TharakaDilshan nice to see someone from UCSC. You saved my day. Thanks a alot. Commented Apr 19, 2019 at 16:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

as i feel when i set the field null even there is a value or not my registration gets completed. But please see the error carefully it says that its trying to insert into (name, email, password, updated_at, created_at) . The query is not trying to insert into the userstatus field. But when it tries to insert data db return an error because usertype field cannot be null. So I this insert into sholud update with usertype. So that I have updated the model with protected fillable fields. But still insert into dosent try to insert into usertype. Thanks for commenting
@nipunrmt have you rerun the migration once you added your field? Because the error gets thrown from the DB connection, which means you don't have that field in the table at all?
I have attached a screen shot of a table user. could you please check it
Can you debug with dd($request->merge([ 'password' => Hash::make($request->get('password')) ])->all()) what does this returns? Because obviously from the error, the usertype is not set even though you have a value of a as I see in the view.
I changed my code as below with dd method dd($request->all()); But still same error . Thanks for your interest on this.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.