DEV Community

Cover image for How to get current user location, city, country in laravel
Hussain Abid
Hussain Abid

Posted on

How to get current user location, city, country in laravel

In this tutorial, we will see how you can get the current user location, city, country, country code, postal code, zip code, region, longitude and latitude using IP Address in laravel.

So we will be using stevebauman/location package in order to get that information.

So let’s get started.

Step 1: Install Laravel

First, we will install laravel into the project. I am installing it in user_location folder.

composer create-project --prefer-dist laravel/laravel user_location
Enter fullscreen mode Exit fullscreen mode

Step 2: Install stevebauman/location package

Then we need to install the stevebauman/location package.

composer require stevebauman/location
Enter fullscreen mode Exit fullscreen mode

Step 3: Add the Service Provider and Alias

Then we need to add the service provider in config/app.php in providers array:

Stevebauman\Location\LocationServiceProvider::class,
Enter fullscreen mode Exit fullscreen mode

And also add following alias in aliases in config/app.php:

'Location' => Stevebauman\Location\Facades\Location,
Enter fullscreen mode Exit fullscreen mode

So your final providers array will look like this:

Alt Text

And aliases array will look like this:

Alt Text

Read Complete Blog Post Here

Top comments (0)