【问题标题】:Integrity constraint violation: 1048 Column 'lawyer_id' cannot be null违反完整性约束:1048 列 'lawyer_id' 不能为空
【发布时间】:2019-08-05 15:35:50
【问题描述】:

在数据库中存储数据时出错..

我将数据存储在数据库中,但它给出了错误,即违反完整性约束:1048 Column 'lawyer_id' cannot be null "

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; 行中出现错误。

【问题讨论】:

  • 尝试将其从 $lawyer-&gt;id 更改为 $lawyer['id']

标签: database laravel-5.7


【解决方案1】:

你在 $data->lawyer_id = $lawyer->id 的两行中创建 $lawyer ($lawyer = new Lawyer()),因此您需要在将其分配给 $data 之前设置 $lawyer 参数。

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 2018-03-04
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    相关资源
    最近更新 更多