1. 快速获得记录条数
$medalCnt = MedalUser: : select ( DB : : raw ( 'count(*) as medal_cnt' ) )
- > where ( 'uid' , $this - > uid )
- > where ( 'medal_id' , $medalId )
- > value ( 'medal_cnt' ) ;
2. 减少某项值
$affected = Wallet: : where ( 'uid' , $this - > uid ) - > where ( 'coin_id' , 12 )
- > decrement ( 'total' , $medal [ 'exchange_num' ] ) ;
if ( ! $affected ) {
throw new \ Exception( '变更失败' ) ;
}
3. 增加某项值
$affected = MedalUserLiveness: : where ( 'uid' , $this - > uid )
- > increment ( 'base_liveness_cnt' , $medal [ 'liveness' ] ) ;
if ( ! $affected ) {
return '更新失败' ;
}
4. 结合
$list = DB : : table ( 'medal_user_liveness' )
- > join ( 'member' , 'medal_user_liveness.uid' , '=' , 'member.uid' )
- > where ( 'real_name_auth' , $authCondition )
- > whereIn ( 'member.uid' , $direct_invite_uids )
- > orderByDesc ( 'medal_user_liveness.liveness_total' )
- > select ( 'medal_user_liveness.*' , 'member.username' , 'member.regtime' , 'member.avatar' )
- > paginate ( $pagesize ) ;
return $this - > success ( $list - > getCollection ( ) ) ;
5. update
$affected = MedalUserLiveness: : where ( 'uid' , $referrerUid ) - > update (
[ 'league_liveness_cnt' = > $leagueLiveness , 'hero_liveness_cnt' = > $heroLiveness ] ) ;
if ( ! $affected ) {
return '更新失败' ;
}
6. 根据一个表插入多条数据
WalletCoin: : valid ( ) - > get ( ) - > map ( function ( $item ) use ( $new_user ) {
$new_user - > wallet ( ) - > save ( new Wallet ( [ 'uid' = > $new_user - > uid , 'coin_id' = > $item [ 'id' ] ] ) ) ;
} ) ;
date ( "Y-m-d H:i:s" )
> 2020 - 08 - 30 10 : 32 : 50
7.取某条记录的某个字段值
$setted_value = Setting: : where ( 'id' , 50 ) - > value ( 'content' ) ;
if ( ! $setted_value ) {
return $this - > error ( 0 , '获取错误' ) ;
}
8. 记录存在与否检测
if ( Member: : where ( 'username' , $data [ 'username' ] ) - > exists ( ) ) {
return $this - > error ( 1 , '用户名已注册,可直接登录' ) ;
}
if ( Wallet: : where ( 'uid' , $this - > uid ) - > where ( 'total' , '>=' , $transNum ) - > doesntExist ( ) ) {
return $this - > error ( 1 , '余额不够' ) ;
}
9. 分页
$pagesize = 10 ;
$walletLog = WalletCoinLog: : where ( 'uid' , $this - > uid ) - > where ( 'coin_id' , 123 )
- > select ( DB : : raw ( 'note,IF(value>0,1,0) as plus,value,inputtime' ) )
- > orderByDesc ( 'inputtime' )
- > paginate ( $pagesize ) ;
return $this - > success ( $walletLog - > getCollection ( ) ) ;