Error Field doesn't have a default value in laravel 5.3

19,150

Solution 1

You should add ->nullable() or ->default('somethingHere') to fields which you send empty values.

$table->string('family')->nullable(); //this means that if you send empty value this field will become MySQL NULL

Or set default value:

$table->string('family')->default('default value here');

Than remigrate:

php artisan migrate:rollback

and

php artisan migrate

Solution 2

I found my solutions here is the link: Eloquent create says column has no default value

The answer is at the bottom, i just quoted the answer also

imbhavin95 replied 8 months ago

The reason this is happening now, however, is that Laravel 5.1 uses strict mode for MySQL by default.

If you would like to revert to previous behavior, update your config/database.php file and set 'strict' => false for your connection.

credits to this man https://laravel.io/user/imbhavin95

Solution 3

You can make it nullable:

$table->string('family')->nullable();

Or add some default value:

$table->string('family')->default('none');

After that you should back up data and run:

php artisan migrate:refresh                                      

Then restore the data.

Or you could create a separate migration and just change family to a nullable:

Schema::table('users', function (Blueprint $table) {
    $table->string('family')->nullable()->change();
});
Share:
19,150

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have no problem in Laravel 5.2 but in Laravel 5.3 after create migration for user model, It shows me following error:

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

    In Model user:

    protected $fillable = [
        'name', 'email', 'password', 'family', 'mobile', 'address', 'status'
    ];
    

    In Migration:

    Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('family');
            $table->string('mobile')->unique();
            $table->string('address');
            $table->boolean('status');
            $table->string('email')->unique();
            $table->string('password');
            $table->integer('reagent');
            $table->rememberToken();
            $table->timestamps();
        });
    

    Where is my problem?

  • Admin
    Admin over 7 years
    Hi, I add family in $fillable after password
  • Admin
    Admin over 7 years
    no any problem in Laravel 5.2 ! Is there another way?
  • Buglinjo
    Buglinjo over 7 years
    Do you have remigrated?
  • Admin
    Admin over 7 years
    Thank you for help me, but in laravel 5.2 dont use ->nullable()
  • Admin
    Admin over 7 years
    what do you mean remigrated?
  • Alexey Mezenin
    Alexey Mezenin over 7 years
    @Ali it couldn't work in 5.2 without the error. When you'll use any method which uses mass assignment feature (like create() or update()), you'll get the error in both 5.2 and 5.3.