如何更新 Laravel 4 中的字段列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19608629/
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
How to update a column of a field in Laravel 4?
提问by nayiaw
I'm trying to call an AJAX request on a route that calls this function updateImpression() in UserController, and this function tries to increment the counter column named "impressions_cpunt" in my 'users' table stored in MySQL. I tried using Eloquent to update the column of one particular user only.
我正在尝试在调用 UserController 中的此函数 updateImpression() 的路由上调用 AJAX 请求,并且此函数尝试增加存储在 MySQL 中的“用户”表中名为“impressions_cpunt”的计数器列。我尝试使用 Eloquent 仅更新一个特定用户的列。
public function updateImpression()
{
$username = Input::get('username');
$user = User::where('username', '=', $username)->first();
$impressions_count = $user->impressions_count + 1;
// save() method failed to update my database.
// $user->impressions_count = $impressions_count;
// $user->save();
//update() method does not return anything. Any syntax error here?
//$user->update(array('impressions_count' => $impressions_count));
DB::table('users')
->where('username', $username)
->update(array('impressions_count' => $impressions_count));
$user = User::where('username', '=', $username)->first();
return $user->impressions_count;
}
I think save() function should be work at the first place but my database doesn't get updated and return the original count, and the update() function doesn't even returning any value from the function. This only works when I use DB but I think Eloquent should be better to use since this is Laravel. Is there anything I missed or something wrong with the syntax? I know increments() data type should be used but please allow me to solve this before I change anything.
我认为 save() 函数应该首先起作用,但我的数据库没有更新并返回原始计数,并且 update() 函数甚至没有从函数返回任何值。这仅在我使用 DB 时有效,但我认为 Eloquent 应该更好用,因为这是 Laravel。有什么我遗漏的地方或语法有问题吗?我知道应该使用 increments() 数据类型,但请允许我在更改任何内容之前解决此问题。
Update:This is my database queries shown in Debugbar when I use save() method:
更新:这是我使用 save() 方法时在 Debugbar 中显示的数据库查询:
select * from 'users' where 'username'= <test> limit 1
select count(*) as aggregate from 'users' where 'username'= <test>
select count(*) as aggregate from 'users' where 'email'= <[email protected]>
回答by cheelahim
User object (which supposed to be Eloquent child) like any other entity will store its state after changes if your save() call was successful. Therefore, there is no need to update the record explicitly. In your case I would go with something like this:
如果您的 save() 调用成功,则用户对象(应该是 Eloquent 子对象)与任何其他实体一样将在更改后存储其状态。因此,无需显式更新记录。在你的情况下,我会用这样的东西:
public function updateImpression()
{
$username = Input::get('username');
$user = User::where('username', '=', $username)->first();
$user->impressions_count += 1;
$user->save();
return $user->impressions_count;
}
回答by Md Mazedul Islam Khan
In Laravel 5.2 you can update specific column by using the following Eloquent builder:
在 Laravel 5.2 中,您可以使用以下 Eloquent 构建器更新特定列:
public function update(Request $request, $id) {
$user = User::where('id', $id)->update(['name', $request->name]);
return redirect()->back()->with('status', trans('User has been updated successfully.'))
}