在 laravel 中具有雄辩关系的更新期间从空值创建默认对象

Posted

技术标签:

【中文标题】在 laravel 中具有雄辩关系的更新期间从空值创建默认对象【英文标题】:Creating default object from empty value during update with eloquent relationship in laravel 【发布时间】:2018-09-09 08:24:05 【问题描述】:

我正在尝试更新“employee”和“employee_details”,其中我使用了一对一的关系。在更新期间,我在 $employeeDetail->card_no = $request->card_no, 中恰好遇到了这个错误,我搜索并尝试实施可能的解决方案,但仍然收到此错误。有人可以帮我解决这个问题吗?

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.');
    

【问题讨论】:

【参考方案1】:

改变

来自

$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,
                                        ]);

$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,
    ]);

【讨论】:

以上是关于在 laravel 中具有雄辩关系的更新期间从空值创建默认对象的主要内容,如果未能解决你的问题,请参考以下文章

laravel-5.4 - 错误:从空值创建默认对象

groupBy 在 laravel 中具有雄辩的关系

Laravel 5 具有雄辩的关系回调函数返回错误记录

Laravel 雄辩模型如何从关系表中获取数据

Laravel 查询构建器与雄辩的关系

Laravel 和雄辩的返回结果来自多对多关系