![]() |
Find Out Geolocation Using IP Address in Laravel |
Installation of Laravel GeoIP Library
For getting started, you need to install the Laravel Geoip library. Install the library using the command:
composer require torann/geoip
Upon library installation, register the service provider with your application. Open config/app.php
and add service providers to the providers array.
1 2 3 4 5 | 'providers' => [ ...... \Torann\GeoIP\GeoIPServiceProvider:: class , ] |
Next, add the facade in the aliases array.
1 2 3 4 5 | 'aliases' => [ .... 'GeoIP' => \Torann\GeoIP\Facades\GeoIP:: class , ]; |
After the above steps, publish the configuration using the command:
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
This command publishes a configuration file to config/geoip.php
.
Find Out Geolocation Using IP Address
We are all set to get Geolocation details using an IP address. The library provides a method to which you need to pass the actual IP address. In return, you get the Geolocation information of an IP address.
Place the below code in your application to fetch details of an IP.
1 2 3 4 | $arr_ip = geoip()->getLocation( 'YOUR_IP_ADDRESS_HERE' ); print_r( $arr_ip ); echo $arr_ip ->country; // get a country echo $arr_ip ->currency; // get a currency |
While using the library, it can be possible you will get the error ‘This cache store does not support tagging’ with BadMethodCallException. If so, just change the below line from config/geoip.php
file.
Replace
1 | 'cache_tags' => [ 'torann-geoip-location' ], |
With
1 | 'cache_tags' => [], |
Clear the configuration cache using the command:
php artisan config:cache
Now try again to run your code. This time you should get the output.
I hope you got to know how to integrate GeoIP on the Laravel website. Please share your thoughts and suggestions in the comment section below.
Method 2.
https://www.positronx.io/how-to-get-location-information-with-ip-address-in-laravel/
إرسال تعليق