1

I am using the following code to get User's IP info dynamically :

public function index(Request $request)
{
        $userIp = $request->ip();
        $locationData = \Location::get($userIp);
        
        return view('welcome',compact('locationData'));
}

And Code in my view page :

{{$locationData->countryName}}  

But this returning the error :

ErrorException Trying to get property 'countryName' of non-object (View: E:\laravel3\Currency\resources\views\welcome.blade.php)

It works nicely for static IP though . For following code it's returning "United States" :

public function index(Request $request)
{
        $userIp = '100.10.0.5';
        $locationData = \Location::get($userIp);
        
        return view('welcome',compact('locationData'));
}
Shishir
  • 187
  • 1
  • 8
  • Are you testing locally, possible to `$request->ip() === '127.0.0.1'`? – Tpojka Feb 05 '21 at 22:19
  • yes, I am testing locally . – Shishir Feb 05 '21 at 22:47
  • 2
    So I assume package can't get any location from fake address (i.e. `127.0.0.1`). Try to test on remote/online server and I think you'll get satisfying result. – Tpojka Feb 05 '21 at 22:59
  • oh, thanks for the suggestion. I did not know "Stevebauman" does not work in localhost – Shishir Feb 05 '21 at 23:04
  • 1
    You should stick with code form examples. [First example of usage](https://github.com/stevebauman/location#usage) has check which checks if object is not null. – Tpojka Feb 05 '21 at 23:08

1 Answers1

2

I use the code given below, which returns all the information dynamically:

use Stevebauman\Location\Facades\Location;
public function index(Request $request) {
    $locationData = Location::get('https://'.$request->ip()); // https or http according to your necessary.
        
    return view('welcome',compact('locationData'));
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83