Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Geo Genius Laravel Package

devrabiul/laravel-geo-genius

Laravel GeoGenius adds IP-based geolocation, automatic timezone detection/conversion, locale detection with translation and number conversion, plus a country picker and phone formatting/validation. Works with Livewire and supports cookies or headers.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the package:
    composer require devrabiul/laravel-geo-genius
    
  2. Publish config and migrations:
    php artisan vendor:publish --provider="Devrabiul\LaravelGeoGenius\LaravelGeoGeniusServiceProvider"
    
  3. Run migrations (if adding timezone columns):
    php artisan migrate
    

First Use Case: Detect User Location

// In a controller or service
$geoData = laravelGeoGenius()->geo()->locateVisitor();
dd($geoData); // Returns array with country, timezone, IP, etc.

First Use Case: Phone Input Field (Blade)

<!DOCTYPE html>
<html>
<head>
    {!! laravelGeoGenius()->initIntlPhoneInput() !!}
</head>
<body>
    <input type="tel" id="phone" name="phone">
</body>
</html>

First Use Case: Timezone Conversion

$convertedTime = laravelGeoGenius()->timezone()->convertToUserTimezone('2024-01-01 12:00:00');

Implementation Patterns

Core Workflow: User Geo-Detection

  1. Initial Detection (on first request):
    $geoData = laravelGeoGenius()->geo()->locateVisitor();
    
  2. Session Persistence (subsequent requests):
    $geoData = laravelGeoGenius()->geo()->getLocationData();
    
  3. Fallback Handling:
    $geoData = laravelGeoGenius()->geo()->getLocationData([
        'fallback' => [
            'country' => 'us',
            'timezone' => 'America/New_York'
        ]
    ]);
    

Livewire Integration Pattern

// In Livewire component
public function mount() {
    $this->geoData = laravelGeoGenius()->geo()->getLocationData();
}

public function updatedGeoData() {
    // React to changes (e.g., user manually selects country)
    laravelGeoGenius()->geo()->updateLocationData($this->geoData);
}

Translation Workflow

  1. Add language support:
    php artisan geo:add-language bn
    
  2. Generate translations:
    php artisan geo:translations-generate --locale=bn
    
  3. Use in code:
    echo geniusTrans('welcome_message');
    

Phone Input Validation

// In your JS file
const input = document.querySelector("#phone");
const iti = window.intlTelInput(input, {
    initialCountry: document.querySelector('.system-default-country-code').dataset.value,
    // other options...
});

form.addEventListener('submit', (e) => {
    if (!iti.isValidNumber()) {
        e.preventDefault();
        alert('Invalid phone number');
    }
});

Timezone Migration Pattern

// Add timezone column to users table
php artisan geo:add-timezone-column users
// In User model
protected static function boot() {
    parent::boot();

    static::creating(function ($user) {
        $user->timezone = laravelGeoGenius()->timezone()->getUserTimezone();
    });
}

Gotchas and Tips

Common Pitfalls

  1. Session vs Cache Conflicts:

    • GeoGenius v1.6+ prioritizes session data over cache. Clear sessions when testing:
      php artisan session:clear
      
    • Tip: Use laravelGeoGenius()->geo()->forceRefresh() to bypass session cache.
  2. IP Detection Issues:

    • Localhost (127.0.0.1) returns external IP by default. For testing:
      laravelGeoGenius()->geo()->setTestIp('8.8.8.8');
      
    • Gotcha: Cloudflare/VPN users may get incorrect geolocation. Implement manual override:
      laravelGeoGenius()->geo()->setLocationData(['country' => 'us']);
      
  3. Translation Quirks:

    • Auto-translation requires auto_translate: true in config.
    • Tip: Use geniusTranslateNumber() for locale-specific number formatting:
      echo geniusTranslateNumber(12345, 'bn'); // Bengali digits
      
  4. Phone Input Edge Cases:

    • Ensure intl-tel-input CSS/JS is loaded before initialization.
    • Debugging: Check for system-default-country-code span in DOM.

Debugging Tips

  1. Inspect Raw Data:
    dd(laravelGeoGenius()->geo()->getRawLocationData());
    
  2. Enable Debug Mode:
    laravelGeoGenius()->setDebug(true);
    // Check Laravel logs for detailed API responses
    
  3. Cache Issues:
    • Clear cache after config changes:
      php artisan cache:clear
      php artisan config:clear
      

Extension Points

  1. Custom IP Provider:
    laravelGeoGenius()->geo()->setIpProvider(function() {
        return request()->ip() ?? 'custom_ip_here';
    });
    
  2. Override Detection Logic:
    laravelGeoGenius()->geo()->extendDetection(function($data) {
        $data['custom_field'] = 'value';
        return $data;
    });
    
  3. Hook into Timezone Conversion:
    laravelGeoGenius()->timezone()->extendConversion(function($date, $timezone) {
        // Modify conversion logic
        return $date->setTimezone($timezone);
    });
    

Performance Optimization

  1. Disable Auto-Detection (for non-geo apps):
    laravelGeoGenius()->geo()->disableAutoDetection();
    
  2. Increase Cache TTL (for stable regions):
    laravelGeoGenius()->geo()->setCacheTtl(1440); // 24 hours
    
  3. Batch API Calls (for bulk operations):
    laravelGeoGenius()->geo()->batchLocate(['ip1', 'ip2', 'ip3']);
    

Configuration Quirks

  1. Phone Input Restrictions:

    • Set only_countries_mode: true to restrict dropdown:
      'phone_input' => [
          'only_countries_mode' => true,
          'only_countries_array' => ['us', 'ca', 'uk']
      ]
      
    • Note: Initial country must be in only_countries_array.
  2. Locale Fallback:

    • Configure fallback locales in config/laravel-geo-genius.php:
      'language' => [
          'fallback_locales' => ['en', 'bn'],
          'default_locale' => 'en'
      ]
      
  3. Timezone Database:

    • GeoGenius uses PHP's built-in timezone database. For custom timezones:
    laravelGeoGenius()->timezone()->addCustomTimezone('Custom/Zone', 'UTC+5');
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport