laravel5.* 字段递增/递减以及实现多字段递增/递减

Song3556 次浏览0个评论2018年01月31日

laravel单个字段递增或者递减其实官网已给出自增或自减

DB::table('users')->increment('votes');   //votes递增1
DB::table('users')->increment('votes', 5);//votes递增5
DB::table('users')->decrement('votes');   //votes递减1
DB::table('users')->decrement('votes', 5);//votes递减5

您还可以指定要操作中更新其它字段:

DB::table('users')->increment('votes', 1, ['name' => 'John']);//votes递增1且更新name字段

上面那些都是可找到的,但是我们常常需要多个字段递增或者递减,更或者一个加一个减?

一、原始表达式

DB::table('users')
   ->where('id', 1)
   ->update([
       'column1' => DB::raw('column1 + 2'),
       'column2' => DB::raw('column2 - 10'),
       'column3' => DB::raw('column3 + 13'),
       'column4' => DB::raw('column4 - 5'),
   ]);

二、运行原生的 SQL 语句

$affected = DB::update('update users set ? = ?+?, ? = ?+? where name = ?', ['hits','hits',1,'hits','views','views',1,'test']);

当然还有其他很多方法,我这里后续再整理

提交评论

请登录后评论

用户评论

    当前暂无评价,快来发表您的观点吧...

更多相关好文

    当前暂无更多相关好文推荐...