Laravel 9 get locations country name, country code, city name, and address from user IP; In this article, we are going to explain how to get country info city name, postal code and address latitude, longitude from user IP address in Laravel 9 with package tevebauman/location.
Following step below to approche implement how to get location in Laravel
You have to installed Laravel framework. Read this to know how to install Laravel 9 project with database configuration.
We need install a third party package called tevebauman/location to enable Class to retrieve data related to Ip Address. Run following command in your root project
composer require stevebauman/location
Open file app -> config.php
'providers' => [
....
Stevebauman\Location\LocationServiceProvider::class
],
'aliases' => [
....
'Location' => Stevebauman\Location\Facades\Location::class,
],
Run command below to publish config file.
php artisan vendor:publish
Next step, open you command prompt and execute below command to generate a controller file and Class named LocationController.
php artisan make:controller LocationController
Open file app -> Http -> Controllers -> LocationController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Location;
class LocationController extends Controller
{
public function index(Request $request)
{
// For dynamic Ip testing
//$ip = $request->ip();
// For static Ip testing
$ip = "36.68.17.155";
$loc = Location::get($ip);
return view('index',["location"=>$loc]);
}
}
We are going to adding route to point URL and Controller, so open the web.php file and add the following code:
Open file routes -> web.php
<?php
use App\Http\Controllers\LocationController;
....
Route::get('/', [LocationController ::class, 'index']);
We need a web page to display data which is render in HTML file. We are going to create a blade file call index.blade.php
Create file resources -> views -> index.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Get Location via Ip Address - CamboTutorial.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Get Location via Ip Address - CamboTutorial.com</h1>
<div class="card">
<div class="card-header">
<h3>Your IP: {{ $location->ip }}</h3>
</div>
<div class="card-body">
<ul class="list-group">
<li class="list-group-item"><h5>Country Name: {{ $location->countryName }}</h5> </li>
<li class="list-group-item"><h5>Country Code: {{ $location->countryCode }}</h5></li>
<li class="list-group-item"><h5>Region Code: {{ $location->regionCode }}</h5></li>
<li class="list-group-item"><h5>Region Name: {{ $location->regionName }}</h5></li>
<li class="list-group-item"><h5>Area Code: {{ $location->areaCode }}</h5></li>
<li class="list-group-item"><h5>City Name: {{ $location->cityName }}</h5></li>
<li class="list-group-item"><h5>Zip Code: {{ $location->zipCode }}</h5></li>
<li class="list-group-item"><h5>Latitude: {{ $location->latitude }}</h5></li>
<li class="list-group-item"><h5>Longitude: {{ $location->longitude }}</h5></li>
<li class="list-group-item"><h5>City Name: {{ $location->cityName }}</h5></li>
<li class="list-group-item"><h5>Time Zone: {{ $location->timezone }}</h5></li>
</ul>
</div>
</div>
</div>
</body>
</html>
We are going start the Laravel server with following command, so we can test and launch the app via browser URL http://localhost:8000/
php artisan serve
Hope this tutorial help you to implement your project. Have a nice day!
You might Also Like:
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him