错误字段在 laravel 5.3 中没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41750167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:10:12  来源:igfitidea点击:

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

laravellaravel-5.3

提问by acooshot

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

我在 Laravel 5.2 中没有问题,但在为用户模型创建迁移后在 Laravel 5.3 中,它显示了以下错误:

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

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?

我的问题在哪里?

回答by Buglinjo

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

您应该添加->nullable()->default('somethingHere')到您发送空值的字段。

$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

回答by acooshot

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

我在这里找到了我的解决方案链接: Eloquent create say 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.

imbhavin95 回复 8 个月前

然而,现在发生这种情况的原因是 Laravel 5.1 默认情况下对 MySQL 使用严格模式。

如果您想恢复到以前的行为,请更新您的 config/database.php 文件并为您的连接设置 'strict' => false 。

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

归功于此人https://laravel.io/user/imbhavin95

回答by Alexey Mezenin

You can make it nullable:

你可以做到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 familyto a nullable:

或者您可以创建一个单独的迁移并更改family为可空值

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