Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

obink's avatar
Level 1

Laravel get Visitor's IP, Location, and City

I currently making a dashboard page which required tracking the visitors, this page is about, how much this certain page being view, and who saw this page and content inside.

I tried a package named cyrildewit/eloquent-viewable and it working fine for counting the viewer. but I can't seem to find how to check "who are they ?" in this package.

so I installed another package called stevebauman/location, the features it's so promising, we can check the location of visitor's cities, countries, and anything else.

but after running a simple test with this, under my CatalogueController, public function show($product)

$whoIsIp = request()->ip();
        $position = Location::get($whoIsIp);
        dd($position);

this error message displaying in my browser

Stevebauman\Location\Exceptions\DriverDoesNotExistException
The location driver [] does not exist.
http://127.0.0.1:8000/catalogue/1

please help.

this is my full code of that function

public function show($product)
    {
        $product = Product::find($product);
        views($product)->record();

        $images = $product->images;
        $whoIsIp = request()->ip();
        $position = Location::get($whoIsIp);
        dd($position);


        return view('product', compact('product', 'images'));
    }

0 likes
16 replies
obink's avatar
Level 1

hi @automica, I did the publish with php artisan vendor:publish and picking number of stevebauman location location package.

isn't that the same thing?

rodrigo.pedra's avatar

Yes, it has the same the results.

As you published those assets, can you share your ./config/location.php file?

obink's avatar
Level 1

hi @rodrigo.pedra , do you mean by this?

'driver' => Stevebauman\Location\Drivers\IpApi::class,

or do you want to see the whole config?

and by any chances, I just check up something and retrying everything, and I got another error message like this

ErrorException
Non-static method Stevebauman\Location\Location::get() should not be called statically
http://127.0.0.1:8000/catalogue/2

Do you or anyone by any chance know what is this? it wasn't like this before.

Kumaravel's avatar

You have to import the Facade of the Location class. Instead of Stevebauman\Location\Location.

obink's avatar
Level 1

hi @kumaravel and also @laracoft I have these line on top

use Stevebauman\Location\Location;
use Stevebauman\Location\Facades\Location as FacadesLocation;

and I'm having these under the function show

$position = Location::get($whoIsIp);
$poisiton2 = FacadesLocation::get($whoIsIp);

if I dump the $position2 the result is always false

Kumaravel's avatar

Since the first one itself will through error. Remove the Location::get($whoIsIp); And check the $position2 again.

laracoft's avatar

@obink

Add use Stevebauman\Location\Facades\Location; at the top of the code after namespace

laracoft's avatar

@obink

  1. You don't need use Stevebauman\Location\Location;
  2. $poisiton2 is spelt wrongly. It should be $position2
  3. $position2 = FacadesLocation::get($whoIsIp); dd($position2); will work
// use Stevebauman\Location\Location;
use Stevebauman\Location\Facades\Location;

$position = Location::get($whoIsIp);
dd($position);
obink's avatar
Level 1

Yes that was typo. but it still returning false. I just deleted $position1 and use Stevebauman\Location\Location; and still no luck. :/ what happened with this package?

rodrigo.pedra's avatar

Which IP are you testing with?

For localhost IP (127.0.0.1) it will always return false. So to test locally you might want add an if and return a default value

I created a new laravel app and added this package:

laravel new laracasts
cd laracasts
composer require stevebauman/location 
php artisan serve

Then on the ./routes/web.php routes file I added these routes:

<?php

use Illuminate\Support\Facades\Route;
use Stevebauman\Location\Facades\Location; // <<<< Using the Façade here

Route::get('/google', function () {
    // Google Public DNS
    return Location::get('8.8.4.4');
});

Route::get('/local', function () {
    // Localhost
    dd(Location::get('127.0.0.1'));
});

Route::get('/request', function () {
    // IP from request
    // will be false locally, as it is
    // the same as 127.0.0.1
    dd(Location::get(request()->getClientIp()));
});

Navigating to http://127.0.0.1:8000/google , which have Google's Public DNS server IP hard-coded I get:

{
  "ip": "8.8.4.4",
  "countryName": "United States",
  "countryCode": "US",
  "regionCode": "VA",
  "regionName": "Virginia",
  "cityName": "Ashburn",
  "zipCode": "20149",
  "isoCode": null,
  "postalCode": null,
  "latitude": "39.03",
  "longitude": "-77.5",
  "metroCode": null,
  "areaCode": "VA",
  "driver": "Stevebauman\Location\Drivers\IpApi"
}

Navigating to http://127.0.0.1:8000/local , I get:

false

And Navigating to http://127.0.0.1:8000/request , from my machine I also get

false

The reason is when I request from my local machine to a local server my IP is 127.0.0.1.

This might be your issue. To test locally you might want to add if block to test if the result is false, and the client IP is 127.0.0.1 then you'd use a default value.

obink's avatar
Level 1

Hi again @rodrigo.pedra, ya I Just realized it yesterday, about test it in local would come false return.

// Returns instance of Stevebauman\Location\Position on success, otherwise false (for example for local network ip addresses)

but then I tried testing it in here $position = Location::get('192.168.1.1'); and it still returning false.

honestly, I never try Location::get in route, because my aim is to record the IP and Cities when somebody hit the product's details public function show

Do you or anyone else manage to get this Location::get($position) inside the controller successfully?

and another thing is what should I import in my controller actually? Is it the facades or the location/location?

rodrigo.pedra's avatar
Level 56

IPs starting with 192.168 are reserved for local network usage, thus they fall into the “local network IP adresses” category which will still return false.

Local IPs adress ranges can also start with: 10, 169.254 , and in the range 172.16-172.31

Reference: https://en.wikipedia.org/wiki/Private_network

And of course 127.0.0.1 is used internally by your local machine.

and another thing is what should I import in my controller actually? Is it the facades or the location/location?

Importing and using the Façade is the easy way to go.

If you want, for whatever reason, to avoid using the Façade, you need to new up an instance of the Location class and call the get method as an instance method:

$location = new \Stevebauman\Location\Location();

$record = $location->get(request()->getClientIp());

Hope it helps.

1 like
obink's avatar
Level 1

it helps, like a lot. thanks, @rodrigo.pedra. I tested it in many different static IPs from each country and it works, now what I need to do just to store the city and region into my database. once again. thanks.

1 like

Please or to participate in this conversation.