2016-12-11 160 views
0

は私が私のコントローラで、これらのエラー[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

エラーメッセージは、uploaderNULLであってはならないと伝えます。

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: images.uploader 
あなたは、 mutatorsを使用し、あなたの Imageモデルでこれを置くことができ、

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

OR

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

代わりに、あなたの移行を変更し

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

私が見ている通り、空でない文字列をデフォルト値にする必要があります。 はそうですか? –

+0

うん、基本的には、あなたのデータベースに正しいデータがあることを確認するための制約があります。あなたがそれが空であることを期待するなら、あなたはそれを述べるべきです – SteD

+0

助けをありがとう^ _ ^ –

関連する問題