SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn’t have a default value

Asked

Viewed 3,544 times

-1

I am trying to start a project in Aravel and am getting this error while selecting sex when registering

"SQLSTATE[HY000]: General error: 1364 Field 'gender' doesn’t have a default value"

return User::create([
            
            'name' => $data['name'],
            'avatar' => $avatar_path,
            'gender' => $data['gender'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

If I put the option "Gender" Null in my phpMyAdmin works without errors, however it does not show sex and yes "NULL"

My html:

<div class="form-group row">
                        <label for="name" class="col-md-4 col-form-label text-md-right">Gender</label>

                          <div class="col-md-6">
                          <select name="gender" class="col-md-4 control-label">
                            <option value="male" name="male">Male</option>
                            <option value="female" name="female">Female</option>
                            </select>

                                  @error('gender')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                               </div>
                               </div>

2 answers

0

Eloquent’s create() method receives an array with the values to be entered in massassign mode. If a property is not contained in the $fillable array of the entity in question, this value is thrown away.

Considering the explanation, almost sure you did not include 'gender' in the $fillable property of the User entity.

So the gender value is not being sent to insert into the database, where you try to insert a row without gender which is a required column of the table, causing the SQL error.

Simply go to your User class and add all the properties that will be saved in the database in the $fillable property array

Edit:

I recommend that you read the documentation of the framework before using it. This is very simple and is well explained in its documentation. If you have problems with English, you can use the translator, because the text is very literal and does not lose its readability when translated by translators.

0

This error is usually caused by you not specifying all values while Mysql/mariadb Strict Mode is active.

To deactivate, open my.cnf or my.ini and add the line sql_mode= Or run the command to temporarily disable:

SET GLOBAL sql_mode = '';

If you do not want to disable Strict mode, just specify all values that are not null in the database.

More details in the documentation of mysql!

  • I forgot to mention that I had already done this, in fact I had no Strict mode and ready, I’m still in error

  • Run the following command and post the result: SELECT @@GLOBAL.sql_mode; If possible also post the Laravel db configuration

  • Showing 0 - 0 records (1 total, Query took 0.0000 seconds.) is still the same. Migration users is this https://i.ibb.co/Tt1qvmm/migation.png

  • What do you mean? You didn’t return anything? try to configure the Laravel as follows: 'Connections' => [ 'mysql' => [ 'Strict' => false, 'modes' => [] ] ]

  • Here I have array for profile picture of each sex and return. They work correctly, but for this reason 'Gender' has to be NULL in phpmyadmin and 'Gender' is NULL https://i.ibb.co/Lxq7m8x/Return.png

  • I got it, the word 'Gender' was missing in Protect fillable

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.