我试图将表(Branch)中的表(Company)主键id值作为外键。并且表(公司,分支)中的主键id值在表(Medicine)中作为外键。但是当我提交这些医学表格时,错误就是Occured:
SQLSTATE [23000]:完整性约束违规:1048列 'company_id'不能为空(SQL:插入
medicines
(company_id
,branch_id
,medicines_name
,type
,potency
,updated_at
,created_at
)值(,1,panadol,平板电脑,23毫克, 2017-02-27 13:03:01,2017-02-27 13:03:01))
控制器:
class MedicineController extends Controller
{
public function getmedicineform(){
$company = Company::all();
$branch = Branch::all();
return view("medicinesdetails.addProduct")->with('company', $company)->with('branch',$branch);
}
public function saveMedinfo(Request $request){
$validator = Validator::make($request->all(),[
'product_name' => 'required',
'product_type' =>'required',
'potency' =>'required',
]);
if($validator->passes()){
$med = new Medicine();
$med->company_id = $request->company_id;
$med->branch_id = $request->branch_id;
$med->medicines_name = $request->product_name;
$med->type = $request->product_type;
$med->potency = $request->potency;
if($med->save()){
$request->session()->flash('message','Successfully Save!!');
return redirect('/add/product');
}
}else{
return redirect('/add/product')->withErrors($validator)->withInput();
}
}
查看:
<form class="form-horizontal product-form" action="/add/product" method="post">
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
@foreach($company as $compny)
<input type="hidden" name= company_id" value="{{$compny->id}}">
@endforeach
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
@foreach($branch as $brnch)
<input type="hidden" name="branch_id" value="{{$brnch->id}}">
@endforeach
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Product Name</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="product_name" placeholder="Product Name">
@if($errors->has('product_name'))
{{$errors->first('product_name')}}
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Product Type</label>
<div class="col-sm-9">
<select class="form-control" name="product_type">
<option value="">Select</option>
<option value="tablet">Tablet</option>
<option value="syrup">Syrup</option>
<option value="injectable">Injectable</option>
</select>
@if($errors->has('product_type'))
{{$errors->first('product_type')}}
@endif
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Potency</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="potency" placeholder="Potency">
@if($errors->has('potency'))
{{$errors->first('potency')}}
@endif
</div>
</div>
@if(Session::has('message'))
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<div class="alert alert-info" role="alert">{{Session::get('message')}}</div>
</div>
</div>
@endif
<div class="form-group">
<label class="col-sm-3 control-label"></label>
<div class="col-sm-9">
<button type="submit" value="Add product" class="btn btn-default">Submit</button>
</div>
</div>
{{csrf_field()}}
</form>