在数据库中存储数据时出错。.
我将数据存储在数据库中,但是它给出了错误,表明违反完整性约束:1048列'lawyer_id'不能为空”
public function lawyersDiplomaInfo(Request $request){
$dipInstitution = $request->get('diploma_name');
$dipYear = $request->get('diploma_year');
$all_params = $request->keys();
$total_dipFile = preg_grep("/^dip_file_.*/", $all_params);
if($dipInstitution != null && $dipYear != null){
$lawyer = new Lawyer();
$data = new LawyerDiploma();
$data->lawyer_id = $lawyer->id;
$data->diploma_institution = $dipInstitution;
$data->year = $dipYear;
$data->created_at = Carbon::now();
$data->updated_at = Carbon::now();
$data->save();
}
for($i = 0; $i < count($total_dipFile); $i++){
if($request->file('dip_file_'.$i) != null){
$filename = $request->file('dip_file_'.$i)->store('diplomafile');
$files = new DiplomaFile();
$files->diploma_id = $data->id;
$files->file = $filename;
$files->save();
}
}
return ['message' => 'Data Added'];
}
在 $ data-> lawyer_id = $ lawyer-> id; 行中,它给出了错误。
答案 0 :(得分:1)
在$ data-> lawyer_id = $ lawyer-> id的两行中创建$ lawyer($ lawyer = new Lawyer()),因此需要将$ lawyer参数设置为$ data。