clock  Mon - Sun 8.00 AM - 8.00 PM
play store

How to Get Current User Location in Laravel?

writter  Sumit Dey Sarkar
Date  07 May 2023
Language  Laravel
How to Get Current User Location in Laravel?

How to Get Current User Location in Laravel?

In this article we will learn how to get current user location in Laravel.


How to Get Current User Location in Laravel


How to find user current location, city, country, country code, zip code,  postal code, region name, ISO code, IP address, latitude, longitude in Laravel

You are going to learn how to get the current location of a user with their IP address by using the Steve Bauman Location Package in this article. Getting a user's location based on their IP Address may be accomplished with the help of the stevebauman/location Laravel package. This makes it easy to find out where a user is based on their IP address. The following types of location information can be retrieved with the assistance of the location library:


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


Laravel Get Current User Location Tutorial

In this we will get current user location by follow below steps.


Step 1 – Install Laravel

Step 2 – Establishing a Connection to the Database

Step 3 – Install the stevebauman/location library

Step 4 – Create Route

Step 5 – Create Controller

Step 6 – Create Blade File

Step 7 – Run


Step 1 – Install Laravel

Start a new Laravel project. This is the first thing that needs to be done. Then, use the code below to download and install Laravel.

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


Step 2 – Establishing a Connection to the Database

The next step is to link the MySQL database and the Laravel program together. After making the database, we'll need to set the database passwords in the application's.env file.



Step 3 – Install the stevebauman/location library

In this stage of the process, we will be using the composer dependency management to install the stevebauman/location Package. Installing the stevebauman/location Package can be done by using the following command.

composer require stevebauman/location


Now, Go to config directory and open app.php file here and register this package into laravel app by add the below code in app.php file:

'providers' => [
'aliases' => [
    'Location' => 'Stevebauman\Location\Facades\Location',


Step 4 – Create Routes

Once this is done, we will need to put in information about routes in the routes/web.php file. Let's get started by opening the routes/web.php file and adding the routes listed below to it.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

| Web Routes
| This section of the application is where web routes
| can be registered for your application. These routes are
| loaded by the RouteServiceProvider within a
| group that also contains the "web" middleware group.
| This group is called "web" middleware group. 
| Now go make something truly amazing!
Route::get('display-user', [UserController::class, 'index']);


Step 5 – Create Controller

Now, using the command that is provided below, let's create a controller that we will call UserController.

php artisan make:controller UserController


Now, open the UserController.php file located in the app/Http/Controllers directory. After that, edit your UserController.php file to include the following line of code:

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 6 – Create Blade File

Creating the blade view file is the task at hand for this stage. Create a user.blade.php file by navigating to the resources/views directory. Then insert the code that is shown below into it:

<!DOCTYPE html>
    <meta charset="utf-8">
    <link href="" rel="stylesheet">
<div class="container">
    <h1>How to Get Current User Location with Laravel</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>


Step 7 – Run

Now that our demonstration is ready to be executed, let's get the development server up and running by using the following artisan command:

php artisan serve


Now, in order to view the output, open the following URL in your browser:



How to Get Current User Location in Laravel?


Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website