Requirements
- PHP >= 7.3
- PHP cURL extension enabled
- Laravel >= 5.0
Installation
Install location using composer require
:
composer require stevebauman/location
Add the service provider in config/app.php
:
Important: If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.
Stevebauman\Location\LocationServiceProvider::class
Publish the configuration file (this will create a location.php
file inside the config/
directory):
php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"
Usage
Retrieve a client's location
use Stevebauman\Location\Facades\Location;
if ($position = Location::get()) {
// Successfully retrieved position.
echo $position->countryName;
} else {
// Failed retrieving position.
}
Note:
- This method retrieves the user's IP address via
request()->ip()
.- In the default configuration,
testing.enabled
is active, the returned IP address is in the USA. Disable it to get the client's real IP address.
Retrieve the location of a specific IP address
$position = Location::get('192.168.1.1');