Laravel 更新方法不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46115064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel update method does not exist
提问by Mike Thrussell
According to the docs, I should be able to update records using update()
https://laravel.com/docs/5.4/queries#updates, but I'm getting the error Method update does not exist
.
根据文档,我应该能够使用update()
https://laravel.com/docs/5.4/queries#updates更新记录,但出现错误Method update does not exist
。
Client::findOrFail($id)->update($request->all());
Any idea why?
知道为什么吗?
回答by HosseyNJF
I think this is because you are using the query builder's method on a single model object. You cannot do this because the findOrFail
method returns a single object that has nothing to do with query builder's methods.
我认为这是因为您在单个模型对象上使用查询构建器的方法。您不能这样做,因为该findOrFail
方法返回与查询构建器的方法无关的单个对象。
Do it like this: Client::findOrFail($id)->first()->fill($request->all())->save();
像这样做: Client::findOrFail($id)->first()->fill($request->all())->save();
回答by Muhammad Rizwan
Try This:
尝试这个:
Client::find($id)->update($request->all());
Or you can use this
或者你可以使用这个
Client::where('id',$id)->first()->update($request->all());