devrabiul/laravel-geo-genius
Laravel GeoGenius adds IP geolocation, automatic timezone detection/conversion, locale & translation helpers, number conversion, and a country picker with phone formatting/validation. Works with Livewire and supports cookies or headers for detection.
composer require devrabiul/laravel-geo-genius
php artisan vendor:publish --provider="Devrabiul\LaravelGeoGenius\LaravelGeoGeniusServiceProvider"
php artisan migrate
// In a controller or middleware
$geoData = laravelGeoGenius()->geo()->locateVisitor();
dd($geoData); // Returns array with country, city, timezone, etc.
<!-- Auto-detect and display user's country -->
<p>You're visiting from {{ laravelGeoGenius()->geo()->getCountry() }}</p>
<!-- Phone input with country picker -->
{!! laravelGeoGenius()->initIntlPhoneInput() !!}
<input type="tel" name="phone" id="phone">
Middleware Integration (Recommended):
// app/Http/Middleware/GeoDetection.php
public function handle(Request $request, Closure $next) {
laravelGeoGenius()->geo()->locateVisitor();
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\GeoDetection::class,
],
];
Livewire Component Integration:
// app/Http/Livewire/GeoAwareComponent.php
public function mount() {
$this->geoData = laravelGeoGenius()->geo()->locateVisitor();
}
Timezone Conversion:
$localTime = laravelGeoGenius()->timezone()->convertToUserTimezone(now());
// In your form submission handler
const phoneInput = document.querySelector('#phone');
const iti = window.intlTelInput(phoneInput, {
initialCountry: document.querySelector('.system-default-country-code').dataset.value,
// other options...
});
if (!iti.isValidNumber()) {
return alert('Invalid phone number');
}
// Auto-translate messages
echo geniusTrans('welcome_message');
// Number formatting (e.g., 12345 → "১২,৩৪৫" for Bengali)
echo geniusTranslateNumber(12345);
// config/laravel-geo-genius.php
'phone_input' => [
'only_countries_mode' => true,
'only_countries_array' => ['us', 'ca', 'gb'],
],
Session vs Cache Conflicts:
php artisan cache:clear
laravelGeoGenius()->geo()->forceRefresh() to bypass cache.Localhost Development:
127.0.0.1. For testing, set:
// config/laravel-geo-genius.php
'ip_detection' => [
'force_ip' => '8.8.8.8', // Test with Google's IP
],
Livewire State Persistence:
public function mount() {
$this->geoData = laravelGeoGenius()->geo()->getCachedData();
}
Inspect Raw Data:
dd(laravelGeoGenius()->geo()->getRawLocationData());
Timezone Issues:
php artisan geo:add-timezone-column users
dd(laravelGeoGenius()->timezone()->getUserTimezone());
Phone Input Not Loading:
initIntlPhoneInput() is called before the input field in Blade.intl-tel-input CSS/JS is loaded (package handles this automatically).Custom IP Provider:
// config/laravel-geo-genius.php
'ip_providers' => [
'ipwhois' => [
'enabled' => true,
'url' => 'https://ipwho.is/json/{ip}',
],
'ipapi' => [
'enabled' => false, // Disable default
],
],
Override Detection Logic:
// app/Providers/AppServiceProvider.php
public function boot() {
laravelGeoGenius()->extend(function ($app) {
$app->geo->setCustomDetection(function () {
return ['country' => 'custom', 'timezone' => 'America/New_York'];
});
});
}
Add Custom Countries:
// config/laravel-geo-genius.php
'countries' => [
'custom' => [
'name' => 'Custom Country',
'dial_code' => '999',
'flag' => '🇨🇺', // Custom flag emoji
],
],
Disable Auto-Detection (for non-geo features):
laravelGeoGenius()->geo()->disableAutoDetection();
Increase Cache TTL (for stable regions):
// config/laravel-geo-genius.php
'cache' => [
'ttl_minutes' => 1440, // 24 hours
],
Lazy Load Phone Input:
@if(request()->wantsJson() || request()->ajax())
{!! laravelGeoGenius()->initIntlPhoneInput() !!}
@endif
How can I help you explore Laravel packages today?