Skip to content

How To Get Current User Location In Laravel (Free IP Geolocation API)

Do you need to know how to get the location of a user in Laravel? Then this article is perfect for you

Laravel is a free, open-source PHP web framework built by Taylor Otwell that is based on Symfony and is meant for the creation of web applications that follow the model–view–controller (MVC) architectural paradigm. Its source code is available on GitHub and is released under the MIT License. 

Laravel’s features include a modular packaging system with a dedicated dependency manager, several methods for accessing relational databases, tools that help in application deployment and maintenance, and an emphasis on syntactic sugar. It was designed in an attempt to give a more advanced alternative to the CodeIgniter framework, which lacked features such as built-in support for user authentication and authorisation.

How To Get Current User Location In Laravel (Free IP Geolocation API)

However, before getting into Laravel and using its features, you’ll need to find a software that provides you with an IP geolocation API, which will then give you your users locations. This is the mapping of an IP address from a connected device to the geographic location of the internet. By geographically mapping the IP address, it provides location information such as the nation, state, city, zip code, latitude/longitude, ISP, area code, and other information.

APIs may be located online by utilizing API-finding tools. Many of them are free to use, but not all of them function in the same manner. As a result, you should exercise caution in selecting one because making the wrong choice might cost you time and money.

We strongly advise you to utilize ipXapi, one of the best IP geolocation APIs available, to save time and simplify the procedure. This instrument may offer real-time IP data because it is linked to many channels. The API’s database is updated daily, with up to 24 database modifications each day.

How To Get Current User Location In Laravel (Free IP Geolocation API)

To use it, you have to:

  1. Create an account at https://ipxapi.com/.
  2. Enter the IP address from which you want data gathered.
  3. Send it out and see how many responses you get.
  4. Save the API to your PC and use it whenever you need it.

How To Use This In Laravel

  1. Install the API
  2. Configure Config file: After installing the software, you must configure the configuration file. So, in the config/app.php file, add a service provider and an alias.
  3. Publish Optional Vendor: You must also create a public configuration file by running the following command; after running this command, you will locate the config/location.php file.
  4. Now if you need the country name ? just print like below and that’s it!
Route::get('details', function () {

	$ip = '50.90.0.1';
    $data = \Location::get($ip);
    dd($data->countryName);

});

About ipXapi

In terms of IP statics, ipXapi has a high level of accuracy. This is due to its extensive database and incredibly useful API. These are linked to several well-known ISPs, who frequently provide information on new and existing IP ranges. Furthermore, the API is regularly updated as a result of its connection to a large number of channels that provide real-time IP data.

After 11 years of continuous optimization, the infrastructure that supports ipXapi and the API has evolved to be incredibly scalable and efficient, handling between 2 and 3 billion API calls every day. Data coverage is constantly increasing, with this platform presently supporting over 2 million unique locations in over 200.000 cities across the world.


Also published on Medium.

Published inAppsTechnology
%d bloggers like this: