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, and a country picker with phone formatting/validation for Laravel. Works with Livewire and supports cookies or headers for detection.

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 using timezone storage):
    php artisan migrate
    

First Use Case: Detect User Location

// In a controller or middleware
$geoData = laravelGeoGenius()->geo()->locateVisitor();
dd($geoData->country, $geoData->timezone, $geoData->latitude);

Quick Blade Integration

<!-- Auto-detect country and initialize phone input -->
{!! laravelGeoGenius()->initIntlPhoneInput() !!}
<input type="tel" name="phone" id="phone">

Implementation Patterns

1. Middleware for Global Geo Detection

// 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,
    ],
];

2. Timezone-Aware Models

// User model
protected static function boot() {
    parent::boot();
    static::creating(function ($user) {
        $user->timezone = laravelGeoGenius()->geo()->getTimezone();
    });
}

3. Livewire Component Integration

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

4. Phone Validation Workflow

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

5. Translation System

// In views
<p>{{ geniusTrans('welcome_message') }}</p>

// In controllers
$translated = geniusTranslateNumber(12345); // Converts to locale-specific digits

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

    • Issue: Geo data not persisting across requests
    • Fix: Ensure session() driver is configured in .env and middleware runs before your routes
    • Tip: Use laravelGeoGenius()->geo()->forceRefresh() to bypass cache/session
  2. Localhost Development

    • Issue: Returns 127.0.0.1 instead of public IP
    • Fix: Configure in .env:
      GEO_GENIUS_LOCALHOST_IP=your_public_ip
      
  3. Timezone Column Migration

    • Gotcha: Forgetting to run migrations after publishing
    • Solution: Run php artisan migrate or manually add the column:
      Schema::table('users', function (Blueprint $table) {
          $table->string('timezone')->nullable()->after('email');
      });
      
  4. Phone Input Initialization

    • Issue: Missing country dropdown
    • Fix: Ensure initIntlPhoneInput() is called before the input field in Blade
    • Debug: Check browser console for 404 on utils.js (verify CDN URL in config)

Debugging Tips

  1. Inspect Raw Geo Data

    dd(laravelGeoGenius()->geo()->getRawData());
    
  2. Check Session Storage

    dd(session()->all());
    
  3. Validate API Responses

    • Test with curl to verify API endpoints:
      curl https://ipwho.is/your_ip
      
  4. Disable Caching Temporarily

    laravelGeoGenius()->setCacheEnabled(false);
    

Performance Optimization

  1. Cache Configuration

    // config/laravel-geo-genius.php
    'cache' => [
        'enabled' => true,
        'ttl_minutes' => 10080, // 7 days
    ],
    
  2. Bulk Timezone Updates

    php artisan geo:add-timezone-column users
    php artisan geo:update-timezones users
    

Extension Points

  1. Custom Geo Data Processing

    laravelGeoGenius()->extend(function ($geo) {
        $geo->getCustomData = function() {
            return $this->getRawData()['custom_field'] ?? null;
        };
    });
    
  2. Override Default APIs

    // config/laravel-geo-genius.php
    'geo' => [
        'api_url' => 'https://your-custom-api/geo',
    ],
    
  3. Custom Phone Validation Rules

    laravelGeoGenius()->phone()->addValidationRule('custom_rule', function($phone) {
        return str_starts_with($phone, '+1');
    });
    
  4. Locale Fallback Chain

    laravelGeoGenius()->language()->setFallbackChain(['bn', 'en']);
    

Configuration Quirks

  1. Country Code Mappings

    • Verify config/laravel-geo-genius.php countries array matches your needs
    • Add custom entries:
      'countries' => [
          'custom' => [
              'name' => 'Custom Country',
              'dial_code' => '999',
          ],
      ],
      
  2. Timezone Database

    • The package uses PHP's built-in timezone database
    • For custom timezones, extend via:
      laravelGeoGenius()->timezone()->addCustomTimezone('Custom/TZ', 'Custom Timezone');
      
  3. Translation System

    • Generated translations go to resources/lang/{locale}/messages.php
    • Use php artisan geo:translations-generate to auto-detect missing keys

Livewire-Specific Tips

  1. Persistent Geo Data

    // In Livewire component
    public $geoData;
    
    public function mount() {
        $this->geoData = laravelGeoGenius()->geo()->locateVisitor();
    }
    
    public function updatedGeoData() {
        // React to changes
    }
    
  2. Timezone-Aware Livewire

    public function getTimezoneOptions() {
        return laravelGeoGenius()->timezone()->getAllTimezones();
    }
    
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor