How to get country from ip in laravel?

Member

by dana , in category: PHP Frameworks , 5 months ago

How to get country from ip in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jasen , 5 months ago

@dana 

To get the country from an IP address in Laravel, you can use a package like "torann/geoip". Here's how you can do it:

  1. Install the package using composer:
1
composer require torann/geoip


  1. Publish the package configuration file:
1
php artisan vendor:publish --provider="TorannGeoIPGeoIPServiceProvider" --tag=config


  1. Update the configuration file "config/geoip.php" with the correct database path:
1
'database_path' => storage_path('app/geoip.mmdb'),


  1. To get the country from an IP address, you can use the GeoIP facade like this:
1
2
3
4
use TorannGeoIPFacadesGeoIP;

$ip = '192.168.1.1';
$country = GeoIP::getLocation($ip)->country;


That's it! You can now use the "country" variable to get the country from the IP address.