MySql Error: 1364 Field 'display_name' doesn't have default value

后端 未结 7 1823
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 21:37

I have just switched from a MAMP installation to a native Apache, MySql and PHP installation. I have got everything working, but I have started using my web app in the new e

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 21:54

    I also faced that problem and there are two ways to solve this in laravel.

    1. first one is you can set the default value as null. I will show you an example:

      public function up()
      {
          Schema::create('users', function (Blueprint $table) {
              $table->increments('id');
              $table->string('name');
              $table->string('gender');
              $table->string('slug');
              $table->string('pic')->nullable();
              $table->string('email')->unique();
              $table->string('password');
              $table->rememberToken();
              $table->timestamps();
          });
      }
      

    as the above example, you can set nullable() for that feature. then when you are inserting data MySQL set the default value as null.

    1. second one is in your model set your input field in protected $fillable field. as example:

      protected $fillable = [
          'name', 'email', 'password', 'slug', 'gender','pic'
      ];
      

    I think the second one is fine than the first one and also you can set nullable feature as well as fillable in the same time without a problem.

提交回复
热议问题