Search code examples
phplaraveleloquentrelationshipslaravel-query-builder

Creating default object from empty value during update with eloquent relationship in laravel


I am trying to update 'employee' and 'employee_details' in which I used one to one relationship. During the update, I am getting this error exactly in $employeeDetail->card_no = $request->card_no, in employeeDetail and I searched and tried to implement the possible solutions but still getting this error. Would someone help me to solve this problem, please?

EmployeeController.php

public function update(Request $request, $id)
{
    $employee = Employee::find($id);
    $employee->card_no = $request->card_no;
    $employee->name = $request->name;
    $employee->father_name = $request->father_name;
    $employee->mother_name = $request->mother_name;
    $employee->spouse_name = $request->spouse_name;
    $employee->permanent_add = $request->permanent_add;
    $employee->present_add = $request->present_add;
    $employee->area = $request->area;
    $employee->dob = Carbon::parse($request->dob)->format('Y-m-d');
    $employee->blood_group = $request->blood_group;
    $employee->nid_number = $request->nid_number;
    $employee->mobile = $request->mobile;
    $employee->reference_name = $request->reference_name;
    $employee->reference_nid = $request->reference_nid;
    $employee->reference_mobile = $request->reference_mobile;
    $employee->reference_add = $request->reference_add;

    if (! $request->photo == '') {
        $employee->photo = $request->photo;
        if ($file = $request->file('photo')) {
            $extension = $file->getClientOriginalExtension() ?: 'png';
            $folderName = '/uploads/employees/photo';
            $destinationPath = public_path() . $folderName;
            $safeName = str_random(10) . '.' . $extension;
            $file->move($destinationPath, $safeName);
            //delete old photo if exists
            if (File::exists(public_path() . $folderName . $employee->photo)) {
                File::delete(public_path() . $folderName . $employee->photo);
            }
            //save new file path into db
            $employee->photo = $safeName;

        }
    }

    if (! $request->nid_file == '') {
        $employee->nid_file = $request->nid_file;
        if ($file = $request->file('nid_file')) {
            $extension = $file->getClientOriginalExtension() ?: 'png';
            $folderName = '/uploads/employees/nid';
            $destinationPath = public_path() . $folderName;
            $safeName = str_random(10) . '.' . $extension;
            $file->move($destinationPath, $safeName);
            //delete old nid_file if exists
            if (File::exists(public_path() . $folderName . $employee->nid_file)) {
                File::delete(public_path() . $folderName . $employee->nid_file);
            }
            //save new file path into db
            $employee->nid_file = $safeName;

        }
    }

    if (! $request->reference_nid_file == '') {
        $employee->reference_nid_file = $request->reference_nid_file;
        if ($file = $request->file('reference_nid_file')) {
            $extension = $file->getClientOriginalExtension() ?: 'png';
            $folderName = '/uploads/employees/nid';
            $destinationPath = public_path() . $folderName;
            $safeName = str_random(10) . '.' . $extension;
            $file->move($destinationPath, $safeName);
            //delete old reference_nid_file if exists
            if (File::exists(public_path() . $folderName . $employee->reference_nid_file)) {
                File::delete(public_path() . $folderName . $employee->reference_nid_file);
            }
            //save new file path into db
            $employee->reference_nid_file = $safeName;

        }
    }

    if (! $request->character_file == '') {
        $employee->character_file = $request->character_file;
        if ($file = $request->file('character_file')) {
            $extension = $file->getClientOriginalExtension() ?: 'png';
            $folderName = '/uploads/employees/character-certificate';
            $destinationPath = public_path() . $folderName;
            $safeName = str_random(10) . '.' . $extension;
            $file->move($destinationPath, $safeName);
            //delete old character_file if exists
            if (File::exists(public_path() . $folderName . $employee->character_file)) {
                File::delete(public_path() . $folderName . $employee->character_file);
            }
            //save new file path into db
            $employee->character_file = $safeName;

        }
    }

    if ($employee->save()) {

        $employeeDetail = EmployeeDetail::where(['employee_id' => $id])
                                        ->update([
                                            $employeeDetail->card_no = $request->card_no,
                                            $employeeDetail->section_id = $request->section_id,
                                            $employeeDetail->designation_id = $request->designation_id,
                                            $employeeDetail->salarygrade_id = $request->salarygrade_id,
                                            $employeeDetail->joining_date = Carbon::parse($request->joining_date)->format('Y-m-d'),
                                            $employeeDetail->quit_date = $request->quit_date,
                                        ]);


        return back()->with('success', 'Congratiolations! You appointed a new employee.');
    }
}

enter image description here


Solution

  • Change

    From

    $employeeDetail = EmployeeDetail::where(['employee_id' => $id])
                                            ->update([
                                                $employeeDetail->card_no = $request->card_no,
                                                $employeeDetail->section_id = $request->section_id,
                                                $employeeDetail->designation_id = $request->designation_id,
                                                $employeeDetail->salarygrade_id = $request->salarygrade_id,
                                                $employeeDetail->joining_date = Carbon::parse($request->joining_date)->format('Y-m-d'),
                                                $employeeDetail->quit_date = $request->quit_date,
                                            ]);
    

    to

    $employeeDetail = EmployeeDetail::where(['employee_id' => $id])
        ->update([
            'card_no' => $request->card_no,
            'section_id' => $request->section_id,
            'designation_id' => $request->designation_id,
            'salarygrade_id' => $request->salarygrade_id,
            'joining_date' => Carbon::parse($request->joining_date)->format('Y-m-d'),
            'quit_date' => $request->quit_date,
        ]);