我尝试将(表公司)(公司ID - >主键)设置为(表分支)(company_id)中的外键。
控制器:
public function savebranchinfo(Request $request){
$validator = Validator::make($request->all(),[
'name' => 'required|min:5',
'email' =>'required|unique:branch',
'address' =>'required',
'contact' =>'required|min:11',
'open_hours' =>'required',
]);
if($validator->passes()){
$branch = new Branch();
$branch->company_id = $request->company_id;
$branch->name = $request->name;
$branch->email = $request->email;
$branch->address = $request->address;
$branch->contact = $request->contact;
$branch->open_hours = $request->open_hours;
if($branch->save()){
$request->session()->flash('message','Successfully save!!');
return redirect('/add/branch');
}
}else{
return redirect('/add/branch')->withErrors($validator)->withInput();
}
}
}
迁移:
public function up()
{
Schema::create('branch', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('company');
$table->String('name');
$table->String('email');
$table->String('address');
$table->String('contact');
$table->String('open_hours');
$table->timestamps();
});
}
查看:
<input type="hidden" value="company_id{{$company->id}}">
错误:
SQLSTATE [23000]:完整性约束违规:1048列&#39; company_id&#39;不能为空(SQL:插入branch
company_id
,name
,email
,address
,contact
,open_hours
, updated_at
,created_at
)值(,Mahmood Son,mahmoodson @ gmail.com,bilal gunj,04237152734,上午8点至晚上8点,2017-02-25 12:06:35,2017-02-25 12 :06:35))
答案 0 :(得分:0)
在我看来,您忘记了表单中的输入名称:
<input type="hidden" name="company_id" value="{{$company->id}}">