我需要以下方面的一些建议。
我有两次迁移,就像这样。
Schema::create('plates', function (Blueprint $table) {
$table->increments('id');
$table->integer('serial_number');
$table->string('crc-code', 50);
$table->string('reason', 50)->nullable();
$table->softDeletes();
$table->timestamps();
});
还有另外一个
Schema::create('documents', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 25)->unique();
$table->text('description')->nullable();
$table->longText('relative_path');
我像这样设置了多对多关系的数据透视表
Schema::create('document_plate', function (Blueprint $table) {
$table->integer('plate_id')->unsigned()->index();
$table->integer('document_id')->unsigned()->index();
$table->primary(['plate_id', 'document_id']);
$table->timestamps();
});
当采取某种行动时,我使用下面的代码附加一个plate
to a document
$plate = Plate::find(1);
$doc = Document::find(1);
if($plate && $doc) {
$plate->documents()->attach($doc->id);
}
第一次,一切都很好!这document_plate
得到更新。当有相同的情况时会发生错误ids
再做一次。
SQLSTATE[23000]:违反完整性约束:1062 键“PRIMARY”的重复条目“1-1”(SQL:插入document_plate
(created_at
, document_id
, plate_id
, updated_at
)
Now the question
有没有办法避免出现错误并用相同的方法更新表ids
??
或者..我需要在前端设置某种验证,告诉用户(在提交之前)他/她选择相同的id's
已经在表中了。
注意:我使用 AngularJS 进行前端操作。