Laravel返回SQLSTATE [23000]:违反完整性约束:1048列'emp_id'

时间:2020-06-28 06:06:33

标签: angular laravel

请问我是laravel的新手,我想向数据库中插入表单值,但出现此错误。 "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'emp_id' cannot be null"

这是我的laravel代码。

private function saveEmployeeLeave(Request $request)
{
    
    $empInfo = new AsEmployeeLeave();
    $empInfo->emp_id = $request->emp_id;
    $empInfo->department_id = $request->department;
    $empInfo->application_time =  Carbon::parse($request->application_time)->format('Y-m-d');
    $empInfo->type = $request->selectedLeaveType;
    $empInfo->date_from = $request->date_from;
    $empInfo->date_to = $request->date_to;
    $empInfo->comment = $request->comment;
    $empInfo->requester_emp_id = $request->id;
    $empInfo->requester_sign = $request->english_name;
    $empInfo->approval_emp_id = $request->approval_emp_id;
    $empInfo->approval_sign = $request->manager;
    $empInfo->contact = $request->contact;
    $empInfo->hours = $request->hours;
    $empInfo->status = $request->status;

    $empInfo->save();
    return $empInfo;
}

这是我获取数据的角度代码。

submitLeave(){
        this.leaveList.emp_id= this.empData.id;
        this.leaveList.department_id= this.leaveList.department_id;
        if(!this.leaveList.selectedLeaveType){
            this.snotifyService.error("Please select type of leave");
        }  else if(!this.leaveList.date_from){
            this.snotifyService.error("Please select date from");
        } else if(!this.leaveList.date_to){
            this.snotifyService.error("Please select date to");
        } else{
            this.hrService.createLeave(this.leaveList).subscribe(data => {
            if(data.status_code == 200){
                this.snotifyService.success("leave added successfully");
                this.modalService.dismissAll();
            } else {
                this.snotifyService.error(data.message);    
            }
            },error=>{
                this.snotifyService.error("Not able to load the data. Please reload project and try 
again.");    
            });
        }
    }

数据服务正在返回此数据,我想使用laravel将这些数据存储在数据库中

{
    "status_code": 200,
    "total_records": 1,
    "message": "Success",
    "errors": [],
    "data": [
        {
            "employee_id": "ASM1060",
            "department_id": "1",
            "english_name": "francis",
            "manager": "Luis Andres",
            "reports_to": 12,
            "paid_vacation": 15,
            "used_vacation": 0,
            "department": "IT",
            "position_name": "IT Supervisor",
            "status": null,
            "leave_status": "Rejected",
            "leave_eligible": 120,
            "days_eligible": "15 days",
            "remainder_eligible": 0,
            "hours_eligible": 0,
            "leavedays_eligible": "15 days 0 hour(s)",
            "leave_balance": 120,
            "days_balance": "15 days",
            "remainder_balance": 0,
            "hours_balance": 0,
            "leavedays_balance": "15 days 0 hour(s)"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您传递了employee_id,但是在控制器中您可以访问emp_id,这就是为什么laravel显示数据库错误 您可以解决此简单的更改,将名称employee_id更改为emp_id,反之亦然