laravel 4:当更新行时,如何从列的当前值中减去一个? [英] laravel 4: how to subtract one from current value of column when updating rows?
本文介绍了laravel 4:当更新行时,如何从列的当前值中减去一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何执行如下操作:
I was wondering how to perform something like this:
Table::update(array('position'=>'position+1'));
据我所知,laravel 4将position + 1作为一个字符串处理,从而变成0。
As far as I know, laravel 4 handles 'position+1' as a string, thus is becomes 0.
我想执行类似于
UPDATE table SET position = position + 1
我可以用雄辩的方式吗?
Can I do that using eloquent?
编辑:nevermind,doh ..DB :: table('users') - > increment('votes');
nevermind, doh.."DB::table('users')->increment('votes');"
推荐答案
只需使用增量
方法:
DB::table('users')->increment('position');
同样适用于递减
:
DB::table('users')->decrement('rank');
您甚至可以将第二个参数设置为要添加/减去的数量:
You may even set the second parameter to the amount you want to add/subtract:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
另外,如果您需要更新其他列,则将其作为第三个参数传递:
Also, if you need to update other columns along with it, you pass it as the third parameter:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
同样的, Eloquent
以及
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
这篇关于laravel 4:当更新行时,如何从列的当前值中减去一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文