简体   繁体   English

更新错误 - 尝试在 Laravel 5.8 中获取非对象的属性“id”

[英]Update Error - Trying to get property 'id' of non-object in Laravel 5.8

I am using Laravel-5.8 for a Web Application.我将 Laravel-5.8 用于 Web 应用程序。 I am trying to apply Rules and Requests in Laravel Validation.我正在尝试在 Laravel 验证中应用规则和请求。 I have these codes:我有这些代码:

rules规则

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

Controller控制器

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

view blade查看刀片

 <div class="card-body"> <form action="{{route('organization.companies.update', ['id'=>$company->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data"> {{ csrf_field() }} <input name="_method" type="hidden" value="PUT"> <span style="color:blue;"><h4 class="box-title"><b>Company Information</b></h4></span> <hr class="mt-0 mb-40"> <div class="row"> <div class="col-md-6"> <div class="form-group row"> <label class="control-label text-right col-md-3"> Company Code<span style="color:red;">*</span></label> <div class="col-md-9 controls"> <input type="text" name="organization_code" placeholder="Enter company code here" class="form-control" value="{{old('organization_code',$company->organization_code)}}"> </div> </div> </div> <!--/span--> <div class="col-md-6"> <div class="form-group row"> <label class="control-label text-right col-md-3"> Company Name<span style="color:red;">*</span></label> <div class="col-md-9 controls"> <input type="text" name="organization_name" placeholder="Enter company name here" class="form-control" value="{{old('organization_name',$company->organization_name)}}"> </div> </div> </div> <!--/span--> </div> <div class="form-actions"> <div class="row"> <div class="col-md-6"> <div class="row"> <div class="col-md-offset-3 col-md-9"> &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button> <button type="button" onclick="window.location.href='{{route('organization.companies.index')}}'" class="btn btn-default">Cancel</button> </div> </div> </div> </div> </div> </form> </div>

When I clicked on the save button to update, I got this error:当我单击保存按钮进行更新时,出现此错误:

Trying to get property 'id' of non-object试图获取非对象的属性“id”

Then, in the rules this code is highlighted:然后,在规则中突出显示此代码:

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

How do I get it resolved?我该如何解决?

Thank you.谢谢你。

Change改变

public function update(UpdateCompanyRequest $request, $id)

to

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

With your current route $id is just an ID and company doesn't exist.您当前的路线$id只是一个 ID, company不存在。

Your rules are looking for it to be an object.您的规则正在寻找它是一个对象。

If you put OrgCompany $company Laravel will automatically find it for you.如果你OrgCompany $company Laravel 会自动为你找到它。

This also allows you to simplify your entire update method.这还允许您简化整个更新方法。

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

You can also update the signature of your edit method to automatically inject the OrgCompany in the same way.您还可以更新您的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM