码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-07 16:09:13      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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()]);
            
                }
 

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

标签:

原文地址:http://www.cnblogs.com/grkin/p/4945030.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!