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 = AppLoginAudit::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= AppLoginAudit::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