Med*_*die 16 php laravel eloquent laravel-4
我想知道如何执行这样的事情:
Table::update(array('position'=>'position+1'));
Run Code Online (Sandbox Code Playgroud)
据我所知,laravel 4将'position + 1'作为一个字符串处理,因此变为0.
我想表现得像
UPDATE table SET position = position + 1
Run Code Online (Sandbox Code Playgroud)
我能用口才吗?
编辑:没关系,doh .."DB :: table('users') - > increment('votes');"
rmo*_*bis 44
只需使用该increment方法:
DB::table('users')->increment('position');
Run Code Online (Sandbox Code Playgroud)
同样适用于decrement:
DB::table('users')->decrement('rank');
Run Code Online (Sandbox Code Playgroud)
您甚至可以将第二个参数设置为要添加/减去的数量:
DB::table('users')->increment('posts', 5);
DB::table('users')->decrement('likes', 3);
Run Code Online (Sandbox Code Playgroud)
此外,如果您需要更新其他列,请将其作为第三个参数传递:
DB::table('users')->increment('range', 3, array(
'name' => 'Raphael',
'rank' => 10
));
Run Code Online (Sandbox Code Playgroud)
而且同样适用于Eloquent模型,以及:
$firstUser = User::find(1);
$firstUser->increment('height', 0.1, array(
'active' => false
));
Run Code Online (Sandbox Code Playgroud)
小智 6
您也可以像这样使用DB :: raw方法:
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position+1')]);
Run Code Online (Sandbox Code Playgroud)
你也可以用这样的其他操作
DB::table('tablename')->where(your condition)->update(['position' => DB::raw('position * 2')]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17325 次 |
| 最近记录: |