Laravel:添加到列中的现有字段数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18198950/
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: Addition to an existing field data in a column
提问by Arr Yor
Please how do i express this php mysql update code in eloquent
请问我如何用雄辩的方式表达这个php mysql更新代码
mysql_query("UPDATE `some_table` SET `value` = `value` + 1000 WHERE `id` = 1");
or
或者
mysql_query("UPDATE `some_table` SET `value` = `value` + $formdata WHERE `id` = 1");
回答by Abishek
The ideal way to do this is to use the in-built Laravel function increment
这样做的理想方法是使用内置的 Laravel 函数 increment
$model = Some_Model::find( $id );
$model->increment('value',1000);
or
或者
Some_Model::where('id',1)->increment('value',1000);
The documentation for the same is at http://laravel.com/docs/queries#raw-expressions
回答by kodepup
You can just retrieve the model and increment it:
您可以只检索模型并增加它:
$model = Some_Model::find( $id );
$model->value += 1000;
$model->save();
回答by Rubens Mariuzzo
Using Eloquent you can write your queries as follow:
使用 Eloquent,您可以按如下方式编写查询:
SomeTable::where('id', 1)
->update(array('value', DB::raw('value + 1000')));