Thursday, December 26, 2024
Google search engine
HomeLanguagesLaravel 9 Get Current User Location From IP

Laravel 9 Get Current User Location From IP

Laravel 9 get current user location from ip; In this tutorial, we will learn how to get current user location from ip address using stevebauman location package in laravel 9 apps.

The stevebauman/location is an very useful library for detecting a users location by their IP Address, and It made retrieving a user’s location information from IP address exorbitantly effortless.

The complete list of location information that you are going to get based on IP address in laravel app with location library:

  • Country name and code
  • Region name and code
  • City name
  • Zipcode
  • ISO code
  • Postal code
  • Latitude and longitude
  • Metro code

Laravel 9 Get Current User Location From IP

Use the following steps to get current user location from ip address in laravel 9 apps:

  • Step 1 – Download Laravel 9 Application
  • Step 2 – Connecting App to Database
  • Step 3 – Install stevebauman/location Package
  • Step 4 – Create Route
  • Step 5 – Create Controller By Artisan Command
  • Step 6 – Create Blade View
  • Step 7 – Run Development Server

Step 1 – Download Laravel 9 Application

First of all download or install laravel 9 new setups. So, open the terminal and type the following command to install new laravel 9 app into your machine:

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

Step 2 – Connecting App to Database

In this step, set up database with your downloaded/installed laravel 9 apps. So, you need to find .env file and setup database details as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database-name
DB_USERNAME=database-user-name
DB_PASSWORD=database-password

Step 3 – Install stevebauman/location Package

In this step, open again your command prompt and execute the following command on it. To install Install stevebauman/location Package

composer require stevebauman/location

After installing package we have to setup config file. So open config/app.php file and add service provider and alias.

'providers' => [

	....

	Stevebauman\Location\LocationServiceProvider::class,

],

'aliases' => [

	....

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

]

Step 4 – Create Routes

In this step, open web.php file from routes direcotry. And update the following routes into web.php file:

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('display-user', [UserController::class, 'index']);

Step 5 – Create Controller By Artisan Command

In this step, run the following command on command prompt to create controller file:

php artisan make:controller UserController

After that, go to app/http/controllers and open UserController.php file. And update the following code into it:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        /* $ip = $request->ip(); Dynamic IP address */
        $ip = '162.159.24.227'; /* Static IP address */
        $currentUserInfo = Location::get($ip);
          
        return view('user', compact('currentUserInfo'));
    }
}

Step 6 – Create Blade View

In this step, visit resources/views directory and create user.blade.php. Then add the following code into it:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  
<div class="container">
    <h1>How to Get Current User Location with Laravel 9 - Tutsmake.com</h1>
    <div class="card">
        <div class="card-body">
            @if($currentUserInfo)
                <h4>IP: {{ $currentUserInfo->ip }}</h4>
                <h4>Country Name: {{ $currentUserInfo->countryName }}</h4>
                <h4>Country Code: {{ $currentUserInfo->countryCode }}</h4>
                <h4>Region Code: {{ $currentUserInfo->regionCode }}</h4>
                <h4>Region Name: {{ $currentUserInfo->regionName }}</h4>
                <h4>City Name: {{ $currentUserInfo->cityName }}</h4>
                <h4>Zip Code: {{ $currentUserInfo->zipCode }}</h4>
                <h4>Latitude: {{ $currentUserInfo->latitude }}</h4>
                <h4>Longitude: {{ $currentUserInfo->longitude }}</h4>
            @endif
        </div>
    </div>
</div>
  
</body>
</html>

Step 7 – Run Development Server

Finally, open a command prompt and run the following command to start the development server:

php artisan serve

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

http://127.0.0.1:8000/send-sms

Recommended Laravel Tutorials

RELATED ARTICLES

Most Popular

Recent Comments