我为用户创建了一个页面来更新他的公司信息,创建用户时所有公司的默认值为空。当用户填写信息时,出现此错误:
Method Illuminate\Database\Eloquent\Collection::update does not exist
我确定这个错误是因为我的 CompanyController@edit
public function edit(Request $request)
{
$this->validate($request, [
'company_name' => 'alpha|max:50',
'phone' => 'integer|max:50',
'gst_hst_number' => 'integer|max:50',
'country' => 'alpha|max:50',
]);
$companies = Company::where('id', Auth::user()->id)->get();
$companies->update([
'company_name' => $request->input('company_name'),
'phone' => $request->input('phone'),
'gst_hst_number' => $request->input('gst_hst_number'),
'country' => $request->input('country')
]);
return redirect()->route('company.index')->with('info', 'Company information was updated.');
}
我在这个问题上停留了很长时间,很乐意提供有关如何正确更新我公司模型可填写字段的帮助和信息。
最佳答案
如错误消息所述,您正在 collection
上使用更新方法,您必须将选择查询更改为:
$companies = Company::where('id', Auth::user()->id)->first();
因为 get()
方法返回的是一个集合,而不是单个记录。
关于php - 如何在 Laravel 上正确更新模型?方法 Illuminate\Database\Eloquent\Collection::update 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60294705/