Laravel 10 Get Country, City Name & Address From IP Address Tutorial

Laravel 10 Get Country, City Name & Address From IP Address Tutorial

If you want to get county name, city name, state name, postal code, latitude, longitude and address of a user in your Laravel application. So for this you have to take the IP address of the user. With the help of ip address you can get the geolocation of the user.

In this tutorial, you will learn how to get country name, state name, country code, city name, and address from user IP address in Laravel 10 apps using package”stevebauman/location”.

Laravel 10 Get Country, City Name & Address From IP Address Example Tutorial

By using the following steps, you can get country name, state name, country code, city name, and address from user IP address in Laravel 10 apps using package”stevebauman/location”.

  • Step 1 – Installing Laravel 10 Setup
  • Step 2 – Connecting Database to App
  • Step 3 – Installing “stevebauman/location”
  • Step 4 – Add Routes
  • Step 5 – Create Controller By Command
  • Step 6 – Start Development Server

Step 1 – Installing Laravel 10 Setup

In this step, open your terminal or command prompt.

Then execute the following command into it to download and install Laravel 10 app into your local system or live server:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Connecting Database to App

In this step, you need to connect to your Laravel application.

So, visit your project root directory, and find .env file. Then add database details as following:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3 – Installing “stevebauman/location”

In this step, execute the following command on command prompt or terminal to install stevebauman/location package in Laravel 10 app:

composer require stevebauman/location

Then, Go to config directory and open app.php file. And register this package into Laravel 10 app by adding the following code into your app.php file:

'providers' => [

	....

	Stevebauman\Location\LocationServiceProvider::class,

],

'aliases' => [

	....

	'Location' => 'Stevebauman\Location\Facades\Location',

]

After that, execute the following command on terminal to publish config/location.php file:

php artisan vendor:publish

Step 4 – Add Routes

In this step, you need to create or add routes in web.php file.

So, go to routes directory of you laravel apps. Then open web.php file and add the following routes into web.php file:

routes/web.php

use App\Http\Controllers\GeoLocationController;

Route::get('get-address-from-ip', [GeoLocationController::class, 'index']);

Step 5 – Create Controller By Command

Now, open your command prompt and execute the following command on terminal to create a controller by an artisan:

php artisan make:controller GeoLocationController

After that, go to app\Http\Controllers and open GeoLocationController.php file. Then update the following code into your GeoLocationController.php file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Location;

class GeoLocationController extends Controller
{
   
    public function index(Request $request)
    {
            $ip = $request->ip();
            $data = \Location::get($ip);
            dd($data);
    }
}

Step 6 – Start Development Server

Now, execute the following command on terminal to start development server:

php artisan serve

Then open your browser and hit the following url on it:

http://localhost:8000/get-address-from-ip

Conclusion

In this Laravel 10 get country, city, state, zip code, latitude, longitude and address from ip address tutorial, you have learned how to fetch country, city, state, zip code, latitude, longitude and address from ip address in Laravel 10 app.

Recommended Laravel Posts

If you have any questions or thoughts to share, use the comment form below to reach us.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *