devrabiul/laravel-geo-genius
Laravel GeoGenius adds IP geolocation, automatic timezone detection/conversion, locale & translation helpers, and a country picker with phone formatting/validation for Laravel. 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->country, $geoData->timezone, $geoData->latitude);
<!-- Auto-detect country and initialize phone input -->
{!! laravelGeoGenius()->initIntlPhoneInput() !!}
<input type="tel" name="phone" id="phone">
// app/Http/Middleware/DetectGeo.php
public function handle(Request $request, Closure $next) {
laravelGeoGenius()->geo()->locateVisitor();
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\Devrabiul\LaravelGeoGenius\Http\Middleware\DetectGeo::class,
],
];
// User model
protected static function boot() {
parent::boot();
static::creating(function ($user) {
$user->timezone = laravelGeoGenius()->geo()->getTimezone();
});
}
// app/Http/Livewire/GeoAwareComponent.php
public function mount() {
$this->geoData = laravelGeoGenius()->geo()->locateVisitor();
$this->timezone = $this->geoData->timezone;
}
// Controller validation
$validator = Validator::make($request->all(), [
'phone' => [
'required',
function ($attribute, $value, $fail) {
$phone = $request->input('phone');
if (!laravelGeoGenius()->phone()->isValidNumber($phone)) {
$fail('The '.$attribute.' is invalid.');
}
}
]
]);
// In views
<p>{{ geniusTrans('welcome_message') }}</p>
// In controllers
$translated = geniusTranslateNumber(12345); // Converts to locale-specific digits
// config/laravel-geo-genius.php
'phone_input' => [
'only_countries_mode' => true,
'only_countries_array' => ['us', 'ca', 'gb'],
],
Session vs Cache Conflicts
session() driver is configured in .env and middleware runs before your routeslaravelGeoGenius()->geo()->forceRefresh() to bypass cache/sessionLocalhost Development
127.0.0.1 instead of public IP.env:
GEO_GENIUS_LOCALHOST_IP=your_public_ip
Timezone Column Migration
php artisan migrate or manually add the column:
Schema::table('users', function (Blueprint $table) {
$table->string('timezone')->nullable()->after('email');
});
Phone Input Initialization
initIntlPhoneInput() is called before the input field in Bladeutils.js (verify CDN URL in config)Inspect Raw Geo Data
dd(laravelGeoGenius()->geo()->getRawData());
Check Session Storage
dd(session()->all());
Validate API Responses
curl to verify API endpoints:
curl https://ipwho.is/your_ip
Disable Caching Temporarily
laravelGeoGenius()->setCacheEnabled(false);
Cache Configuration
// config/laravel-geo-genius.php
'cache' => [
'enabled' => true,
'ttl_minutes' => 10080, // 7 days
],
Bulk Timezone Updates
php artisan geo:add-timezone-column users
php artisan geo:update-timezones users
Custom Geo Data Processing
laravelGeoGenius()->extend(function ($geo) {
$geo->getCustomData = function() {
return $this->getRawData()['custom_field'] ?? null;
};
});
Override Default APIs
// config/laravel-geo-genius.php
'geo' => [
'api_url' => 'https://your-custom-api/geo',
],
Custom Phone Validation Rules
laravelGeoGenius()->phone()->addValidationRule('custom_rule', function($phone) {
return str_starts_with($phone, '+1');
});
Locale Fallback Chain
laravelGeoGenius()->language()->setFallbackChain(['bn', 'en']);
Country Code Mappings
config/laravel-geo-genius.php countries array matches your needs'countries' => [
'custom' => [
'name' => 'Custom Country',
'dial_code' => '999',
],
],
Timezone Database
laravelGeoGenius()->timezone()->addCustomTimezone('Custom/TZ', 'Custom Timezone');
Translation System
resources/lang/{locale}/messages.phpphp artisan geo:translations-generate to auto-detect missing keysPersistent Geo Data
// In Livewire component
public $geoData;
public function mount() {
$this->geoData = laravelGeoGenius()->geo()->locateVisitor();
}
public function updatedGeoData() {
// React to changes
}
Timezone-Aware Livewire
public function getTimezoneOptions() {
return laravelGeoGenius()->timezone()->getAllTimezones();
}
How can I help you explore Laravel packages today?