'User IP address and location in Laravel 5.5

I am trying to get the ip address and location of user during registration...when user sign up for the first time I want to save the ip address and his location in the database in users table.

Please give me a solution to get the ip address and location of the new user...

Below is the line which i am using but this gives me wrong IP address of localhost...

127.0.0.1

$user = new User([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'company' => $data['company'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'phone' => $data['phone'],
        'country' => $data['country'],
        'zipcode' => $data['zipcode'],
        'city' => $data['city'],
        'state' => $data['state'] == "other" ? $data['custom_state'] : $data['state'],
        'ip_address' => request()->ip(),
    ]);


Solution 1:[1]

    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if (isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    $json     = file_get_contents("http://ipinfo.io/$ipaddress/geo");
    $json     = json_decode($json, true);
    $country  = $json['country'];
    $region   = $json['region'];
    $city     = $json['city'];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adil Waheed