【问题标题】:column does not have a default value laravel列没有默认值 laravel
【发布时间】:2019-12-02 04:51:47
【问题描述】:

当我尝试插入用户归档时出现错误,

profile_image does not have a default value ,但我没有在此列中插入任何值

RegisterController.php

protected function create(array $data)
    {
        return Customer::create([
            'first_name' => $data['name'],
            'email' => $data['email'],
            'customer_password' => hash('sha512', $data['password'])
        ]);
    }

型号

protected $fillable = ['first_name','email','customer_password'];

架构

public function up()
    {
        DB::statement('ALTER TABLE customers MODIFY profile_image varchar(255) DEFAULT NULL');
    }

为什么会这样???

谢谢。

【问题讨论】:

  • 您能添加您的客户表架构吗?
  • @ϻᴇᴛᴀʟ 用架构更新了我的问题
  • 您得到的错误与 ALTER TABLE 语句一致,该语句添加了一个默认的 not 已经运行。因此,在添加新模型之前运行 alter 语句。
  • 您是如何生成客户模型的?是基于Customers 表吗?
  • @ϻᴇᴛᴀʟ 是的.....

标签: mysql insert laravel-6


【解决方案1】:

请像这样传递profile_image的空白字符串位置

protected function create(array $data)
    {
        return Customer::create([
            'first_name' => $data['name'],
            'email' => $data['email'],
            'customer_password' => hash('sha512', $data['password']),
            'profile_image' => ""
        ]);
    }
或者

如果您要更改表,请创建这样的迁移

Schema::table('table_name', function(Blueprint $table) {

    $table->string('profile_imae')->nullable()->change();

});

【讨论】:

  • 但是为什么会这样呢?
  • 因为默认情况下我们没有发送配置文件图像的任何值数据库获取空值,所以错误来了
  • 你只需添加一个空白字符串
  • 但在模型中我定义了可填充的文件
  • 但在可填写字段中 profile_image 不存在
猜你喜欢
  • 2017-08-28
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2019-02-05
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多