我可以更改Eloquent模型主键.
我想设置主键admin_id而不是'id'?
我知道我可以改变模型的表名
protected $table = "admin";
Run Code Online (Sandbox Code Playgroud)
主键有类似的东西吗?
我需要一些关于以下方面的建议.
我有2次迁移,就像这样.
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();
});
Run Code Online (Sandbox Code Playgroud)
而另一个
Schema::create('documents', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 25)->unique();
$table->text('description')->nullable();
$table->longText('relative_path');
Run Code Online (Sandbox Code Playgroud)
我设置的多对多关系的数据透视表就像这样
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();
});
Run Code Online (Sandbox Code Playgroud)
采取某种行动时,我使用下面的代码将a附加plate到adocument
$plate = Plate::find(1);
$doc = Document::find(1);
if($plate && $doc) {
$plate->documents()->attach($doc->id);
}
Run Code Online (Sandbox Code Playgroud)
第一次,一切正常!将document_plate得到更新.ids再次执行相同操作时会发生错误.
SQLSTATE [23000]:完整性约束违规:1062重复条目'1-1'的关键'主要'(SQL:插入
document_plate(created_at,document_id,plate_id,updated_at)
现在的问题
有没有办法避免错误,只是用同样的方法更新表ids?
或者..我需要在前端设置某种验证,告诉用户(在提交之前)他/她选择id's已经在表中的相同内容. …