2017-02-25 151 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

相關問題