laravel5.* 字段递增/递减以及实现多字段递增/递减
Song •
3694 次浏览 •
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']);
当然还有其他很多方法,我这里后续再整理
-
laravel中distinct()的使用方法与去重 2017-09-11
-
Laravel将view缓存为静态html,laravel页面静态缓存 2021-10-09
-
[ laravel爬虫实战--基础篇 ] guzzle描述与安装 2017-11-01
-
[ 配置教程 ] 在ubuntu16.04中部署LNMP环境(php7+maridb且开启maridb远程以及nginx多域名访问 )并配置laravel环境 2017-07-18
-
laravel11如何启用routes/api.php无状态路由 2025-03-06
热门文章
-
laravel11如何启用routes/api.php无状态路由 2025-03-06
-
oppo手机默认浏览器urlscheme 2025-02-13
-
mysql如何给运营人员添加只有查询权限的账号 2024-12-02
-
Mac 安装mysql并且配置密码 2024-11-20
-
阿里云不同账号(跨账号)ECS服务器同地域如何实现免费内网互通? 2024-11-12
更多相关好文