更新错误-试图获取Laravel 5.8中非对象的属性'id'

问题描述 投票:0回答:1

我正在将Laravel-5.8用于Web应用程序。我正在尝试在Laravel验证中应用规则和请求。我有以下代码:

规则

public function rules()
{
    return [
        'organization_code' => [
            'required',
            'string',
            'min:2',
            'max:20',               
            Rule::unique('org_companies', 'organization_code')->ignore($this->route('company')->id)
        ],   
        'organization_name' => [
            'required',
            'string',
            'min:3',
            'max:100',               
            Rule::unique('org_companies', 'organization_name')->ignore($this->route('company')->id)
        ], 
        'website' => [
            'nullable',
            'url',
            'max:100',               
            Rule::unique('org_companies', 'website')->ignore($this->route('company')->id)
        ],             
        'org_image'             => 'nullable|image|mimes:jpeg,bmp,png,gif|max:2048',
        'total_employees'       => 'nullable|numeric|digits_between:0,10',
        'registration_number' => [
            'nullable',
            'string',
            'max:50',               
            Rule::unique('org_companies', 'registration_number')->ignore($this->route('company')->id)
        ], 
        'phone_number' => [
            'nullable',
            'numeric',
            'phone:NG',               
            Rule::unique('org_companies', 'phone_number')->ignore($this->route('company')->id)
        ],            
        'secondary_phone' => [
            'nullable',
            'numeric',
            'phone:NG',               
            Rule::unique('org_companies', 'secondary_phone')->ignore($this->route('company')->id)
        ],    
        'email' => [
            'nullable',
            'email',
            'max:80',               
            Rule::unique('org_companies', 'email')->ignore($this->route('company')->id)
        ],   
        'secondary_email' => [
            'nullable',
            'email',
            'max:80',               
            Rule::unique('org_companies', 'secondary_email')->ignore($this->route('company')->id)
        ],            
    ];
}

控制器

public function edit($id)
{       
    $company = OrgCompany::where('id', $id)->first();   
    $countries = ConfigCountries::all();
    return view('organization.companies.edit')->with('company', $company)->with('countries', $countries);
}

public function update(UpdateCompanyRequest $request, $id)
{
    try {    
            $orgStartDate = Carbon::parse($request->org_start_date);
            $company = OrgCompany::find($id);
            $company->organization_code     = $request->organization_code;
            $company->organization_name     = $request->organization_name;
            $company->website               = $request->website;
            $company->org_description       = $request->org_description;
            $company->total_employees       = $request->total_employees;
            $company->registration_number   = $request->registration_number;
            $company->org_start_date        = $orgStartDate;
            $company->phone_number          = $request->phone_number;
            $company->secondary_phone       = $request->secondary_phone;
            $company->email                 = $request->email;
            $company->secondary_email       = $request->secondary_email;
            $company->country_id            = $request->country_id;  

             if ($request->org_image != "") {
                 $org_image = $request->file('org_image');
                 $new_name = rand() . '.' . $org_image->getClientOriginalExtension();
                 $org_image->move(public_path('storage/companies/image'), $new_name);
                 $company->org_image = $new_name;
            }

        $company->save();
            Session::flash('success', 'Company is updated successfully');
            return redirect()->route('organization.companies.index');                

    } catch (Exception $exception) {
            Session::flash('danger', 'Action failed!');
            return redirect()->route('organization.companies.index');  
    }
}

当我单击保存按钮进行更新时,出现此错误:

试图获取非对象的属性'id'

然后,在规则中此代码突出显示:

Rule :: unique('org_companies','organization_code')-> ignore($ this-> route('company')-> id)

我该如何解决?

谢谢。

laravel rules
1个回答
0
投票

更改

public function update(UpdateCompanyRequest $request, $id)

to

public function update(UpdateCompanyRequest $request, OrgCompany $company)

[使用您当前的路线,$id只是一个ID,company不存在。

您的规则正在寻找它作为对象。

如果输入OrgCompany $company,Laravel会自动为您找到它。

这还可以简化整个更新方法。

public function update(UpdateCompanyRequest $request, OrgCompany $company)
{
    try {    
        $orgStartDate = Carbon::parse($request->org_start_date);

// this will already be assigned and is unnecessary -> 
        // $company = OrgCompany::find($id);

        // your request object already contains a validated, clean array.
        $validated = $request->validated();
        $company->fill($validated);
        $company->org_start_date        = $orgStartDate;

        if ($request->org_image != "") {
            $org_image = $request->file('org_image');
            $new_name = rand() . '.' . $org_image->getClientOriginalExtension();
            $org_image->move(public_path('storage/companies/image'), $new_name);
            $company->org_image = $new_name;
        }

        $company->save();
        Session::flash('success', 'Company is updated successfully');
        return redirect()->route('organization.companies.index');                

    } catch (Exception $exception) {
        Session::flash('danger', 'Action failed!');
        return redirect()->route('organization.companies.index');  
    }
}

您还可以更新edit方法的签名,以相同的方式自动注入OrgCompany。

public function edit(OrgCompany $company)
{       
// already exists now -> $company = OrgCompany::where('id', $id)->first();   
    $countries = ConfigCountries::all();
    return view('organization.companies.edit')->with('company', $company)->with('countries', $countries);
}
© www.soinside.com 2019 - 2024. All rights reserved.