更新错误 - 试图在 Laravel 5.8 中获取 属性 'id' 的 non-object
Update Error - Trying to get property 'id' of non-object in Laravel 5.8
我正在为 Web 应用程序使用 Laravel-5.8。我正在尝试在 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');
}
}
查看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="m-t-0 m-b-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">
<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>
当我点击保存按钮进行更新时,出现了这个错误:
Trying to get property 'id' of non-object
然后,在规则中突出显示此代码:
Rule::unique('org_companies', 'organization_code')->ignore($this->route('company')->id)
如何解决?
谢谢。
改变
public function update(UpdateCompanyRequest $request, $id)
至
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);
}
我正在为 Web 应用程序使用 Laravel-5.8。我正在尝试在 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');
}
}
查看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="m-t-0 m-b-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">
<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>
当我点击保存按钮进行更新时,出现了这个错误:
Trying to get property 'id' of non-object
然后,在规则中突出显示此代码:
Rule::unique('org_companies', 'organization_code')->ignore($this->route('company')->id)
如何解决?
谢谢。
改变
public function update(UpdateCompanyRequest $request, $id)
至
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);
}