Laravel: how to insert in the database asynchronously?

Asked

Viewed 302 times

1

I am developing a site in PHP, using Eloquent for database, and I have a certain problem of slowness in the time of the user to log in to the database.

What I mean is... The user is taking almost 3 seconds to log in, and as I am somewhat of a perfectionist, I am trying to improve this.

The fact is, at the time the user logs in, I write an Index in the database with his login information, (date/time, system used, location and etc... so that the user himself has control of suspected irregular activities in his registration).

Performing some tests, I noticed that the delay is because of this Select (to log in) and then Insert (for login history) that is performed, ie select I know that is necessary and already tested, is very well optimized...

But Insert, I am trying to perform asynchronously, so that PHP sends the command and goes ahead with the command in the background.

Is there a solution in php or from the proper Laravel/eloquent for this?

Edit: Follow function Login in question:

    public function login(){
    $login = Login::select('idUser', 'userUrl', 'trueURL', 'firstName', 'lastName', 'email', 'password_hash')->where('email', '=', $_POST['email'])->first();
    if($login){
        if(password_verify($_POST['password'], $login->password_hash)){
            session_start();
            $_SESSION['user_data'] = ['idUser' => $login->idUser,
                                      'trueURL' => $login->trueURL,
                                      'userUrl' => $login->userUrl,
                                      'firstName' => $login->firstName,
                                      'lastName' => $login->lastName,
                                      'email' => $login->email];

            $now = date('Y/m/d h:i:s', time());
            $browser = get_browser(null, true);
            $guest_ipAddress = $_SERVER['REMOTE_ADDR'];
            $ipDetails = json_decode(file_get_contents("http://ipinfo.io/{$guest_ipAddress}/json"));

            $inputs = [ 'id_user' => $login->idUser,
                        'public_ip' => $guest_ipAddress, 
                        'timestamp' => $now, 
                        'device_platform' => $browser["platform"], 
                        'device_parent' => $browser["parent"],
                        'device_type' => $browser["device_type"],
                        'country' => $ipDetails->country,
                        'region' => $ipDetails->region,
                        'city' => $ipDetails->city
            ];
            $registerLoginHistory = RegisterUserLogin::create($inputs);
            JSON(array('status' => true));
        }else{
            JSON(array('status' => false));
        }
    }else{
        JSON(array('status' => false));
    }
}
  • I believe you’re looking for Jobs/queues

  • I can see your whole routine!?

  • @Marceloboni sorry for the delay, my connection fell and just came back now... I’m a little new at Alavel, so I’m doing a little research on

  • @Virgilionovic added the code in the body of the question! :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.