Home Laravel How To Get Current User Location From IP Address In Laravel?

How To Get Current User Location From IP Address In Laravel?

by Shahriar Sagor
Get Current User Location From IP Address In Laravel

Hello Dev, today now in this quick example, i will teach you laravel 9 get user current location. Here I will explained simply about how to get current user location in laravel 9. From here you will learn laravel 9 get location from ip. Now in this post i will give you a simple example of laravel 9 get user location. Here know How To Get Current User Location From IP Address In Laravel?

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2: Install stevebauman/location Package

here, i will install stevebauman/location package for getting the current location on login user.

composer require stevebauman/location
Step 3: Create Route

Now in this is step i have to create some routes for add to cart function.


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 4: Create Controller

Now in this step, i have to create UserController and then add the following code on that file:


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 = ''; /* Static IP address */
        $currentUserInfo = Location::get($ip);
        return view('user', compact('currentUserInfo'));
Step 5: Create Blade Files

here, i have to create blade file for user. so let’s create one by one files:


<!DOCTYPE html>
    <meta charset="utf-8">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
    <h1>How to Get Current User Location with Laravel - CodingsPoint.com</h1>
    <div class="card">
        <div class="card-body">
                <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>

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook


You may also like