Laravel 8.0 Get Country, City & Address From IP Address example

Hello artisan, Today I am going to show an interesting topic about fetching user info in Larave 8.0. In Laravel 8.0 you can get Country, City, and address is very easy to find using stevebauman/location package.

If you are using the laravel app, you might want to retrieve personal data (country name, country code, area code, city name, zip code, iso code, postal code, latitude, longitude, metro code, metro code) from the laravel 8 app’s from ip address. So, this article will teach you get user details from the IP.

Read Also

Install “stevebauman/location”

composer require stevebauman/location

You can find the official GitHub repository here.

Configuration the “stevebauman/location” package

config/app.php
'providers' => [
    ....
    Stevebauman\Location\LocationServiceProvider::class,
],
 
'aliases' => [
    ....
    'Location' => 'Stevebauman\Location\Facades\Location',
]

Publish the configuration file (this will create a location.php file inside the config/ directory):

php artisan vendor:publish --provider="Stevebauman\Location\LocationServiceProvider"

or by running bellow command

php artisan vendor:publish

Add Routes

Route::get('/get-location',[HomeController::class,'getLocation']);

Controller

app\Http\Controller\sHomeController.php
 public function getLocation(Request $request)
    {

        if ($position = Location::get()) {
            echo $position->countryName;
        } else {
            echo "Can't fetch!";
        }
    }

Run the Application

php artisan serve

visit:

http://127.0.0.1:8000/get-location

Output

United States

Hope, It helps