我想知道如何执行这样的操作:
Table::update(array('position'=>'position+1'));
据我所知,laravel 4 将 'position+1' 作为字符串处理,因此 is 变为 0。
我想执行类似的操作
UPDATE table SET position = position + 1
我可以使用雄辩的方式做到这一点吗?
编辑:没关系,多哈..“DB::table('users')->increment('votes');”
只需使用increment
method:
DB::table('users')->increment('position');
同样适用于decrement
:
DB::table('users')->decrement('rank');
您甚至可以将第二个参数设置为您想要添加/减去的金额:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
此外,如果您需要随之更新其他列,请将其作为第三个参数传递:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
这同样适用于Eloquent
模型,以及:
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)