简体   繁体   English

如何在 Laravel 上正确更新模型? 方法 Illuminate\\Database\\Eloquent\\Collection::update 不存在

[英]How to properly update a model on laravel? Method Illuminate\Database\Eloquent\Collection::update does not exist

I made a page for a user to update his company information, all companies default values are null, when a user is created.我为用户创建了一个页面来更新他的公司信息,当创建用户时,所有公司的默认值都为空。 When a user fills the information, i get this error:当用户填写信息时,我收到此错误:

 Method Illuminate\Database\Eloquent\Collection::update does not exist 

I am sure this error is because of my CompanyController@edit我确定这个错误是因为我的 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.');
}

I am stuck quite some time on this issue, would gladly apriciate help and information how to properly update my company models fillable fields.我在这个问题上被困了很长时间,很乐意提供帮助和信息如何正确更新我的公司模型可填写字段。

‌As the error message says, you are using the update method on a collection , you have to change the select query to this: ‌正如错误消息所说,您在collection上使用更新方法,您必须将选择查询更改为:

$companies = Company::where('id', Auth::user()->id)->first();

Because the get() method returns a collection, not a single record.因为get()方法返回一个集合,而不是单个记录。

Try this.尝试这个。 ->get() is for multiple collections here you can directly update your records. ->get()用于多个集合,您可以直接更新您的记录。

$companies = Company::where('id', Auth::user()->id)->update([
    'company_name' => $request->input('company_name'),
    'phone' => $request->input('phone'),
    'gst_hst_number' => $request->input('gst_hst_number'),
    'country' => $request->input('country')
]);;

Your using update method on collection, but collection doesn't have update method,您对集合使用update方法,但集合没有update方法,

what you need is remove the get() , so you can use update method on eloquent builder:您需要的是删除get() ,因此您可以在 eloquent builder 上使用update方法:

$companies = Company::where('id', Auth::user()->id);
$companies->update([
        'company_name' => $request->input('company_name'),
        'phone' => $request->input('phone'),
        'gst_hst_number' => $request->input('gst_hst_number'),
        'country' => $request->input('country')
]);

暂无
暂无

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

相关问题 Laravel 8:方法 Illuminate\Database\Eloquent\Collection::update 不存在 错误 - Laravel 8: Method Illuminate\Database\Eloquent\Collection::update does not exist ERROR 方法Illuminate \\ Database \\ Eloquent \\ Collection :: update不存在 - Method Illuminate\Database\Eloquent\Collection::update does not exist 错误:“方法 Illuminate\Database\Eloquent\Collection::update 不存在。” 如何将多行数据更新到表中? [雄辩] - Error: "Method Illuminate\Database\Eloquent\Collection::update does not exist." How do I update multiple row of data to table? [Eloquent] 方法 Illuminate\Database\Eloquent\Collection::books 不存在。(laravel) - Method Illuminate\Database\Eloquent\Collection::books does not exist.(laravel) 方法 Illuminate\Database\Eloquent\Collection::skip 不存在 - Laravel 5.8 - Method Illuminate\Database\Eloquent\Collection::skip does not exist - Laravel 5.8 Laravel 8:方法 Illuminate\Database\Eloquent\Collection::latest 不存在 - Laravel 8: Method Illuminate\Database\Eloquent\Collection::latest does not exist 方法 Illuminate\Database\Eloquent\Collection::addEagerConstraints 不存在 LARAVEL - Method Illuminate\Database\Eloquent\Collection::addEagerConstraints does not exist LARAVEL Laravel,使用分页方法 Illuminate\Database\Eloquent\Collection::render 不存在 - Laravel, use pagination Method Illuminate\Database\Eloquent\Collection::render does not exist Laravel方法Illuminate \\ Database \\ Eloquent \\ Collection :: toSql不存在。 错误 - Laravel Method Illuminate\Database\Eloquent\Collection::toSql does not exist. error 错误方法 Illuminate\\Database\\Eloquent\\Collection::save 不存在。 在 Laravel - Error Method Illuminate\\Database\\Eloquent\\Collection::save does not exist. in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM