我有一个表雇员和表联系人在那里雇用一个名为contactid女巫的列是我在联系表中创建数据的关键词我在登记完整性约束时违反但是我归咎于正确的id。
这是我的控制器来处理创造雇员
public function create()
{
return view('employmentform');
}
public function handleCreate(Request $request)
{
$employe = employe::create([
'fname' => $request->input('first_name'),
'lname' => $request->input('last_name'),
'phone' => $request->input('phone_number'),
'email' => $request->input('email'),
'houseno' => $request->input('house'),
'kebele' => $request->input('kebele'),
'city' => $request->input('city'),
'state' => $request->input('state'),
'age' => $request->input('date'),
'username' => $request->input('user_name'),
'sex' => $request->input('sex'),
'password' => Hash::make($request->input('country')),
'salary' => $request->input('salary'),
'bankaccount' => $request->input('account'),
'bankname' => $request->input('bank'),
'employedate' => getdate(),
'contactid'=> 5,
]);
return Redirect::action('ContactController@create');;
}
我在联系人表格中有一行ID为5,但我得到的错误是
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`shop1`.`employes`, CONSTRAINT `employes_contactid_foreign` FOREIGN KEY (`contactid`) REFERENCES `contacts` (`id`) ON DELETE CASCADE) (SQL: insert into `employes` (`fname`, `lname`, `phone`, `email`, `houseno`, `kebele`, `city`, `state`, `age`, `username`, `sex`, `password`, `salary`, `bankaccount`, `bankname`, `updated_at`, `created_at`) values (eu, tseu, 0912378909, eualtse@gmail.com, hdikw, sdo, sjad, jaos, 2007-09-09, euael, Female, y$VhU2iJSuXAoXqsan38Gsve5JGtahhNeYpzeJjAYRnTGAEI1TeBCaK, 3000, 9839408, enb, 2015-05-16 13:07:09, 2015-05-16 13:07:09))
我的表架构
Schema::table('employes', function($table)
{
$table->integer('contactid')->unsigned();
$table->foreign('contactid')->references('id')->on('contacts');
});
答案 0 :(得分:0)
您正在为contactid输入值5,这是您员工表中的froeign键和联系人表中的主键作为id。现在,您将在employee表中输入一个常量值,而不管您是否在联系表中有id 5。通过查看错误,很明显您在联系人表中没有id为5的条目。