Using a user's IP address to obtain location information can provide useful context for your online services. Laravel 10 makes it simple to retrieve such data and enhance user experiences. This tutorial will lead you through the steps of collecting current location information using Laravel 10.
An IP address is a one-of-a-kind identifier for a device on the internet or a local network. IP is an abbreviation for "Internet Protocol," which is a collection of rules that governs the format of data transferred over the internet or a local network.
Laravel's integration of location services enables you to easily obtain a user's approximate geolocation information without requiring complicated preparations. In this post, we'll look at the methods provided by Laravel 10 for collecting location data, discuss the underlying physics, and demonstrate how to use them.
You can also create livewire form components and provide user IP details but here we will only the information.
Let's get this started.
Laravel Installation
Open terminal and run this command to create a laravel project.
$ composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
$ php artisan serve
Assuming laravel already installed inside your system.
Location Composer Package Installation
Open project into terminal and run this composer command,
$ composer require stevebauman/Location
It will install all needed package files into /vendor folder.
Create Location Controller
Go to terminal and run this artisan command to create controller file.
$ php artisan make:controller LocationController
It will create LocationController.php inside /app/Http/Controllers folder.
Open file LocationController.php and write this complete code into it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Stevebauman\Location\Facades\Location;
class LocationController extends Controller
{
public function ip_details()
{
// Put your IP address
$ip = '223.178.210.170';
$data = Location::get($ip);
return view('userinfo', compact('data'));
}
}
Create Blade Template File
Go to /resources/views folder and create a file userinfo.blade.php inside it.
Open userinfo.blade.php and write this complete into it,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 10 Get Current Location Details Tutorial</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" style="margin-top: 20px;">
<h3>Laravel 10 Get Current Location Details Tutorial</h3>
<div class="card">
<div class="card-body">
@if($data)
<h4>IP: {{ $data->ip }}</h4>
<h4>Country Name: {{ $data->countryName }}</h4>
<h4>Country Code: {{ $data->countryCode }}</h4>
<h4>Region Code: {{ $data->regionCode }}</h4>
<h4>Region Name: {{ $data->regionName }}</h4>
<h4>City Name: {{ $data->cityName }}</h4>
<h4>Zip Code: {{ $data->zipCode }}</h4>
<h4>Latitude: {{ $data->latitude }}</h4>
<h4>Longitude: {{ $data->longitude }}</h4>
@endif
</div>
</div>
</div>
</body>
</html>
Add Route
Open web.php file from /routes folder. You need to add a route into it.
//...
use App\Http\Controllers\LocationController;
Route::get('ip-details', [LocationController::class, 'ip_details'])->name('ip_details');
Application Testing
Open project to terminal and type the command to start development server
$ php artisan serve
URL: http://127.0.0.1:8000/ip-details
We hope this article helped you to learn about Laravel 10 Get Current Location Details by IP Tutorial in a very detailed way.
Top comments (0)