How to does a mass update in LARAVEL5 (inside Laravel 5 do a lot of data updates)

Source: Internet
Author: User

Today, I had spent 3 hours to fix one problem, the old program have a bug, originally, when a user profiles don ' t now allow Multi Logon, It would update the other login records of [loginaudit] table of this user,

But the code had a problem, it may not get his final login record correctly, and would update all his login record Includi Ng the final one.

Interestingly, I find out a method which Laravel provide, but never used before, Mass Update on one line of code. Can You image how easy it is.

$audit = \app\loginaudit::where (' Traineruserid ', $username)
                         ->orderby (' id_num ',' DESC ')->firstorfail ();
                Session::p ut (' id_login ', $audit->id_num);
Kick out other login session of same user
                if ' N ')
                {
                    $audit = \app\loginaudit::where (' Traineruserid ', $username)
                                            ->where ('id_num '<>', session (' Id_login '))
                                            ->whereraw (' logoutdatetime is NULL ')->update ([' Logoutdatetime '=>carbon::now ()]);
            
                }

How to does a mass update in LARAVEL5 (inside Laravel 5 do a lot of data updates)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.