【问题标题】:Subtracting fields from two different tables and updating the value during update in Laravel从两个不同的表中减去字段并在 Laravel 中更新期间更新值
【发布时间】:2020-10-14 14:51:24
【问题描述】:

我有两个不同的表,table_employee 和 table_leave。 table_employee 包含 emp_id、paid_vacation(In Hours)、used_vacation(in Hours) 等字段,而 table_leave 包含 id、hours_spent(in Hours)。请问如何从table_employee中的paid_vacation中扣除table_leave中的hours_spent,然后用新值更新table_employee中的used_vacation

例如。 paid_vacation =150 小时,used_vacation = 0,hours_spent= 8 小时,这样,这个 used_vacation 应该更新为 150 -8 小时 = 142 小时。我想在表中扣除并更新。

private function saveEmployeeLeave(Request $request, $id=null){
    
    if($id != NULL){
        $employeeId = DB::table('as_tbl_employee_master')
                            ->where('employee_id', $request->employee_id)
                            ->get();
    }
    $employee_id = $request->employee_id;
    $paid_vacation = $request->paid_vacation;
    $used_vacation = $request->used_vacation;

    $empData = new AsEmployee();
    if($id !== null) {
        $empData = AsEmployee::find($id);
        if($empData == null) {
            $errors = array('Id: ' . $id . ' not exist');
            return json_encode([], 400, 'Failed', $errors);
        }
    }
    $empData->employee_id = $employee_id;
    $empData->paid_vacation = $paid_vacation;
    $empData->used_vacation = $used_vacation;
    $empData->save();
    // $emp_id = $request->emp_id;
    $emp_id = $request->emp_id;
    $dept = $request->department_id;
    $english_name = $request->english_name;
    $department = $request->department;
    $selectedLeaveType = $request->selectedLeaveType;
    $application_time = date('Y-m-d H:i:s');
    $date_from = $request->date_from;
    $date_to = $request->date_to;
    $comment = $request->comment;
    $contact = $request->contact;
    $hours = $request->hours;
    $approval_emp_id = $request->reports_to;
    $manager = $request->manager;
    $status = $request->status;
    $empInfo = new AsEmployeeLeave();
    if($id !== null) {
        $empInfo = AsEmployeeLeave::find($id);
        if($empInfo == null) {
            $errors = array('Id: ' . $id . ' not exist');
            return json_encode([], 400, 'Failed', $errors);
        }
    }
    $empInfo->emp_id = $emp_id;
    $empInfo->department_id = $dept;
    $empInfo->application_time = $application_time;
    $empInfo->type = $selectedLeaveType;
    $empInfo->date_from = $date_from;
    $empInfo->date_to = $date_to;
    $empInfo->comment = $comment;
    $empInfo->requester_emp_id = $emp_id;
    $empInfo->requester_sign = $english_name;
    $empInfo->approval_emp_id = $approval_emp_id;
    $empInfo->approval_sign = $manager;
    $empInfo->contact = $contact;
    $empInfo->hours = $hours;
    $empInfo->status = $status;
    $empInfo->save();
    return $empData;
}

我在这个函数里面调用了它

    public function addEmpLeave(Request $request){
    $responseData = $this->saveEmployeeLeave($request);
    return ($responseData instanceof JsonResponse) ? $responseData : $this->sendResponse($responseData);
}

【问题讨论】:

  • Stack Overflow 不是代码编写服务。我们总是乐于帮助和支持新的编码员,但您需要先帮助自己。您应该尝试自己编写代码。请阅读How to create a Minimal, Reproducible ExampleHow do I ask a good question?
  • @kerbh0lz,确定我写了代码,但我卡在代码中间,这就是我需要帮助的原因,谢谢。我会把我的代码贴出来给你看
  • @Francis,发布您的更新控制器
  • 谢谢@TalhaF。我只是使用 api 调用了该函数,当我从 UI 中单击更新时,它应该调用该函数
  • 您还在寻找解决方案吗?

标签: mysql sql angularjs laravel


【解决方案1】:

试试这个:

private function saveEmployeeLeave(Request $request, $id=null){
    
    if($id != NULL){
        $employeeId = DB::table('as_tbl_employee_master')
                            ->where('employee_id', $request->employee_id)
                            ->get();
    }
    $employee_id = $request->employee_id;
    $paid_vacation = $request->paid_vacation;
    $used_vacation = $request->used_vacation;

    $empData = new AsEmployee();
    if($id !== null) {
        $empData = AsEmployee::find($id);
        if($empData == null) {
            $errors = array('Id: ' . $id . ' not exist');
            return json_encode([], 400, 'Failed', $errors);
        }
    }
    $empData->employee_id = $employee_id;
    $empData->paid_vacation = $paid_vacation;
    $empData->used_vacation = $used_vacation;
    $empData->save();
    // $emp_id = $request->emp_id;
    $emp_id = $request->emp_id;
    $dept = $request->department_id;
    $english_name = $request->english_name;
    $department = $request->department;
    $selectedLeaveType = $request->selectedLeaveType;
    $application_time = date('Y-m-d H:i:s');
    $date_from = $request->date_from;
    $date_to = $request->date_to;
    $comment = $request->comment;
    $contact = $request->contact;
    $hours = $request->hours;
    $approval_emp_id = $request->reports_to;
    $manager = $request->manager;
    $status = $request->status;
    $empInfo = new AsEmployeeLeave();
    if($id !== null) {
        $empInfo = AsEmployeeLeave::find($id);
        if($empInfo == null) {
            $errors = array('Id: ' . $id . ' not exist');
            return json_encode([], 400, 'Failed', $errors);
        }
    }
    $empInfo->emp_id = $emp_id;
    $empInfo->department_id = $dept;
    $empInfo->application_time = $application_time;
    $empInfo->type = $selectedLeaveType;
    $empInfo->date_from = $date_from;
    $empInfo->date_to = $date_to;
    $empInfo->comment = $comment;
    $empInfo->requester_emp_id = $emp_id;
    $empInfo->requester_sign = $english_name;
    $empInfo->approval_emp_id = $approval_emp_id;
    $empInfo->approval_sign = $manager;
    $empInfo->contact = $contact;
    $empInfo->hours = $hours;
    $empInfo->status = $status;
    $empInfo->save();

    $empData = AsEmployee::find($empData->id);
    $empData->used_vacation = (int)$paid_vacation - (int)$hours;
    $empData->save();

    return $empData;
}

【讨论】:

  • 感谢@STA,我认为这应该可以,但我已经解决了,非常感谢
猜你喜欢
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
相关资源
最近更新 更多