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 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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:
    composer require devrabiul/laravel-geo-genius
    php artisan vendor:publish --provider="Devrabiul\LaravelGeoGenius\LaravelGeoGeniusServiceProvider"
    
  2. Run migrations (if using timezone storage):
    php artisan migrate
    

First Use Case: Detect User Location

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

Quick Blade Integration

<!-- 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">

Implementation Patterns

Core Workflow: Geo-Aware Application

  1. 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,
        ],
    ];
    
  2. Livewire Component Integration:

    // app/Http/Livewire/GeoAwareComponent.php
    public function mount() {
        $this->geoData = laravelGeoGenius()->geo()->locateVisitor();
    }
    
  3. Timezone Conversion:

    $localTime = laravelGeoGenius()->timezone()->convertToUserTimezone(now());
    

Phone Validation Pattern

// 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');
}

Translation System

// Auto-translate messages
echo geniusTrans('welcome_message');

// Number formatting (e.g., 12345 → "১২,৩৪৫" for Bengali)
echo geniusTranslateNumber(12345);

Country Restriction Pattern

// config/laravel-geo-genius.php
'phone_input' => [
    'only_countries_mode' => true,
    'only_countries_array' => ['us', 'ca', 'gb'],
],

Gotchas and Tips

Common Pitfalls

  1. Session vs Cache Conflicts:

    • Geo data is cached per IP (7 days by default). Clear cache with:
      php artisan cache:clear
      
    • For testing, use laravelGeoGenius()->geo()->forceRefresh() to bypass cache.
  2. Localhost Development:

    • Package automatically detects external IP for 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
      ],
      
  3. Livewire State Persistence:

    • Geo data persists across Livewire requests via session. For initial load:
      public function mount() {
          $this->geoData = laravelGeoGenius()->geo()->getCachedData();
      }
      

Debugging Tips

  1. Inspect Raw Data:

    dd(laravelGeoGenius()->geo()->getRawLocationData());
    
  2. Timezone Issues:

    • Verify timezone column exists in your users table:
      php artisan geo:add-timezone-column users
      
    • Check current timezone:
      dd(laravelGeoGenius()->timezone()->getUserTimezone());
      
  3. Phone Input Not Loading:

    • Ensure initIntlPhoneInput() is called before the input field in Blade.
    • Verify intl-tel-input CSS/JS is loaded (package handles this automatically).

Extension Points

  1. Custom IP Provider:

    // config/laravel-geo-genius.php
    'ip_providers' => [
        'ipwhois' => [
            'enabled' => true,
            'url' => 'https://ipwho.is/json/{ip}',
        ],
        'ipapi' => [
            'enabled' => false, // Disable default
        ],
    ],
    
  2. 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'];
            });
        });
    }
    
  3. Add Custom Countries:

    // config/laravel-geo-genius.php
    'countries' => [
        'custom' => [
            'name' => 'Custom Country',
            'dial_code' => '999',
            'flag' => '🇨🇺', // Custom flag emoji
        ],
    ],
    

Performance Optimization

  1. Disable Auto-Detection (for non-geo features):

    laravelGeoGenius()->geo()->disableAutoDetection();
    
  2. Increase Cache TTL (for stable regions):

    // config/laravel-geo-genius.php
    'cache' => [
        'ttl_minutes' => 1440, // 24 hours
    ],
    
  3. Lazy Load Phone Input:

    @if(request()->wantsJson() || request()->ajax())
        {!! laravelGeoGenius()->initIntlPhoneInput() !!}
    @endif
    
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.
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui