I am try to set the (Table company )(Company id -> primary key) to the foreign key in (Table branch)(company_id).
Controller:
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();
}
}
}
Migration:
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();
});
}
View :
<input type="hidden" value="company_id">
Error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'company_id' cannot be null (SQL: insert into branch
(company_id
, name
, email
, address
, contact
, open_hours
, updated_at
, created_at
) values (, Mahmood Son, mahmoodson@gmail.com, bilal gunj, 04237152734, 8am-8pm, 2017-02-25 12:06:35, 2017-02-25 12:06:35))
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire