【发布时间】:2017-02-25 12:26:00
【问题描述】:
我正在尝试将 (Table company )(Company id -> primary key) 设置为 (Table branch)(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 列“company_id”不能为空(SQL:插入branch(company_id,name,email,address,contact,@ 987654330@, updated_at, created_at) 值 (, Mahmood Son, mahmoodson@gmail.com, bilal gunj, 04237152734, 8am-8pm, 2017-02-25 12:06:35, 2017-02-25 12: 06:35))
【问题讨论】:
-
company_id 是自增列吗?然后将其从列表中删除。
-
哦,很清楚它显示在分支表中,您正在尝试插入空白公司 ID,这就是问题所在。我的意思是('应该有一些价值',Mahmood Son,等等......