2016-12-11 53 views
0

当我尝试保存到数据库中,我不断收到在我的控制器这些错误Laravel数据保存到数据库模型SQLSTATE [23000]:完整性约束违规:19 NOT NULL约束失败:

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

错误消息告诉你,栏uploader不应该是NULL

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

迁移更改为

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

OR

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

或者,你可以使用mutators,把这个在您的Image模型。

public function setUploaderAttribute($value) 
{ 
    $this->attributes['uploader'] = (is_null($value) ? '': $value); 
} 
+0

我看到所以我不填充的每一列都需要为空或者有一个空字符串的默认值 是对的吗? –

+0

是的,基本上这些限制是为了确保你的数据库中有正确的数据。所以,如果你期望它是空的,你应该说明它 – SteD

+0

好的,谢谢你的帮助^ _ ^ –

相关问题