2017-02-25 91 views
0

我尝试将(Table company)(Company id - > primary key)设置为(Table branch)(company_id)中的外键。完整性约束违规:1048列'company_id'不能为空laravel5.3

控制器:

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 '不能b E无效(SQL:插入branchcompany_idnameemailaddresscontactopen_hoursupdated_atcreated_at)值(,马哈茂德子,[email protected],比拉尔甘吉,04237152734,8 am-8pm,2017-02- 25 12点06分35秒,2017年2月25日12时06分35秒))

+0

COMPANY_ID是自动递增列?然后从列表中删除它。 –

+0

哦清除它显示在分支表中你试图插入空白的公司ID是问题。我的意思是('应该是一些价值',Mahmood儿子等... –

回答

0

在我看来,你在表单忘了输入名称:

<input type="hidden" name="company_id" value="{{$company->id}}"> 
+0

还没有工作! – Haider

+0

请验证$ company-> id有一个值存在于公司表中,只需看看你的html页面源。 – dparoli

相关问题