Laravel使用Model SQLSTATE [23000]将数据保存到数据库:完整性约束违规:19 NOT NULL约束失败:

时间:2016-12-11 10:40:08

标签: php database sqlite laravel

当我尝试保存到数据库时,我不断收到这些错误

PDOException in Connection.php line 479:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: images.uploader


QueryException in Connection.php line 769:
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: images.uploader (SQL: insert into "images" ("name", "description", "updated_at", "created_at") values (coat, description test, 2016-12-11 10:34:34, 2016-12-11 10:34:34))

我的控制器中的商店功能

public function store($name)
{
    $image = new Image;
    $image->name = $name;
    $image->description = "description test";

    $image->save();

}

迁移

public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->increments('id');
        $table->string('uploader');
        $table->string('name');
        $table->string('description');
        $table->integer('width');
        $table->integer('height');
        $table->decimal('size');
        $table->integer('views')->default(0);
        $table->string('extension');
        $table->timestamps();
    });
}

和我的模特

class Image extends Model
{
    protected $fillable = ["name", "description"];
}

我不知道自己做错了什么,我已经坚持了太长时间。 我希望有人可以帮助我

1 个答案:

答案 0 :(得分:1)

错误消息告诉您<img src="https://placehold.it/10x10"> <img src="https://placehold.it/20x20">列不应该是uploader

NULL

将迁移更改为

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: images.uploader

OR

$table->string('uploader')->default('');

或者,您可以使用mutators,将其放在$table->string('uploader')->nullable(); 模型中。

Image