how to do a mass update in Laravel5 ( 在Laravel 5里面怎么做大量数据更新 )

Today, I had spent 3 hours to fix one problem, The old program has a bug, originally, when a user profile  don’t now allow  Multi Logon,  It will update the other login records of [LoginAudit] table of this user,

But the code has a problem, it may not get his final login record correctly,  and will update all his login record including the final one.

Interestingly, I find out a method which Laravel provide, but never used before, Mass Update in One line of code. can you image how easy it is .

$audit = \App\LoginAudit::where('TrainerUserID', $username)
                         ->orderBy('id_num','DESC')->firstOrFail();
 
                Session::put('id_login', $audit->id_num);
 
//kick out other login session of same user
                if($user->MultiLogonAllowed == 'N')
                {
                    $audit= \App\LoginAudit::where('TrainerUserID', $username)
                                            ->where('id_num', '<>', session('id_login'))
                                            ->whereRaw(' LogoutDateTime IS NULL')->update(['LogoutDateTime'=>Carbon::now()]);
            
                }
 

转载于:https://www.cnblogs.com/grkin/p/4945030.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值