在删除或更新表记录之前是否有某种方法可以检查表记录是否具有外键?
我有一个使用删除的代码:
但是我得到这个错误:
“ SQLSTATE [23000]:违反完整性约束:1451无法删除或更新父行:外键约束失败(prueba
。permiso
,CONSTRAINT permiso_id_ap_foreign
外键({ {1}})引用id_ap
(at
))(SQL:从id
删除,其中at
= 5)“
控制器:
id
使用的表:
public function deleteSede(Request $id) {
$sedes=at::find($id->id)->delete();
/* if $sedes thrown a foreign key error do this...else do this other
thing
*/
return redirect("/");
}
html:
Schema::create('sede', function (Blueprint $table) {
$table->integer('id')->autoIncrement();
$table->string("nit",9);
$table->string("rs",40)
; //razon Social
});
Schema::create('permiso', function (Blueprint $table) {
$table->unsignedInteger('id')->autoIncrement();
$table->integer("id_vinp")->unsigned();
$table->integer("id_ap")->default(0)->unsigned();
$table->tinyInteger("orden")->default(0);
$table->tinyInteger("aFinal")->default(0);
$table->tinyInteger("pFinal")->default("0");
$table->foreign("id_vinp")->references("id")->on("v_p");
$table->foreign("id_ap")->references("id")->on("at");
});
Schema::create('v_fpat', function (Blueprint $table) {
$table->unsignedInteger('id')->autoIncrement();
$table->integer("id_vp")->unsigned();
$table->integer("id_at")->unsigned();
$table->integer("id_fp")->unsigned();
$table->foreign("id_vp")->references("id")->on("v_p");
$table->foreign("id_at")->references("id")->on("at");
$table->foreign("id_fp")->references("id")->on("forpago");
});
路线:
<form action="{{route("sede.destroy",["id"=>$sede->id])}}" method="POST">
@method("DELETE")
<input type="submit" value="Delete Sede">
@csrf
</form>