Laravel-5 使用 fill() 更新数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31053613/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:50:12  来源:igfitidea点击:

Laravel-5 using fill() to update database

phplaravellaravel-5

提问by V4n1ll4

I'm trying to use fill()to update a current row in the database. However, it seems to be creating a new row instead of updating each time.

我正在尝试用于fill()更新数据库中的当前行。但是,它似乎是在创建一个新行,而不是每次都更新。

Does anyone know what could be wrong?

有谁知道可能有什么问题?

Here's my code:

这是我的代码:

$reserve->where('cookie_id', 'idCode')->first();
$reserve->fill($request->all())->save();

return Redirect::to('checkout');

回答by pl4g4

Try something similar to this

尝试类似的东西

$user = User::where ('cookie_id', 'idCode'); 
$new_user_data = $request->all();
$user->fill($new_user_data);
$user->save();

You can also try using update()

您也可以尝试使用 update()

$affectedRows = User::where('votes', '>', 100)->update($request->all());

回答by Wader

Judging by your code you've misunderstood how to fetch an instance out the database. See http://laravel.com/docs/5.1/eloquent#retrieving-single-models

根据您的代码判断,您误解了如何从数据库中提取实例。请参阅http://laravel.com/docs/5.1/eloquent#retrieving-single-models

Try the following

尝试以下

$reserve = Reserve::where('cookie_id', $id)->first();

$reserve->fill($request->input())->save();

return redirect()->to('checkout');