Need development support?

Request a quote for your web projects, no matter how big or small it is!

Country detection in Laravel applications

  • avatar
  • 4 mins read

The ability to gather and analyze data about users' geographical locations has become increasingly vital for numerous applications. Whether it's personalizing content, tailoring marketing strategies, or implementing region-specific features, having accurate geolocation information can significantly enhance the user experience. To facilitate this process, GeoDetect package detects the country associated with an IP address.

GeoDetect allows to effortlessly extract valuable country information from IP addresses. Powered by a robust and up-to-date IP geolocation database, the package provides accurate results and ensures reliable performance. By Integrating GeoDetect into your Laravel applications you can easily implement geolocation functionality without the hassle of building and maintaining your own IP geolocation database.


To install the package, execute the following composer command:

composer require hibit-dev/geodetect


Once installed, include the GeoDetect package in your class:

use Hibit\\GeoDetect;

Instantiate the class or use dependency injection:

$geoDetect = new GeoDetect();

Retrieve country record providing user's IP address:

$country = $geoDetect->getCountry('XXX.XXX.XXX.XXX');

Finally, use available helpers to retrieve the required country information:


Self-hosted database

The package utilizes MaxMind's database in the background, which is regularly updated to ensure the accuracy of the data. However, you also have the option to use a self-hosted database by specifying the location of the DB file:

$country = $geoDetect->setCountriesDatabase('location_to_db_file')

Implementation in Laravel

To facilitate testing, we will create a new route within our Laravel application. In the routes/web.php file, a new route called get-country has been added. When this route is accessed, it will return all the available information in JSON format.

Route::get('/get-country', function (Illuminate\\Http\\Request $request) {
$geoDetect = new Hibit\\GeoDetect();

$country = $geoDetect->getCountry($request->getClientIp());

return response()->json([
'geonameId' => $country->getGeonameId(),
'isoCode' => $country->getIsoCode(),
'name' => $country->getName(),
'isInEuropeanUnion' => $country->isInEuropeanUnion(),

Please note that when testing the country detection functionality locally, it may not work as expected. This is because the client IP address in the request instance is typically set to localhost ( Keep this in mind while testing the country detection feature on your local development environment.


GeoDetect is simple but yet powerful PHP package for IP-based country detection. Its seamless integration, accurate results, and efficient performance make it an invaluable tool for developers working on projects that require geolocation functionality. By incorporating country detection into Laravel applications, developers can enhance user experiences and ensure the delivery of region-specific content, ultimately making their applications more inclusive and globally accessible.


Official GitHub:

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month


Leave a Reply

Your email address will not be published.

Replying to message:

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.