psicologiaymente/cloudflare-geoip

This package is abandoned and no longer maintained. The author suggests using the palauaandsons/cloudflare-geoip package instead.

Cloudflare GeoIP helper with Laravel

1.2.1 2023-11-15 06:50 UTC

This package is auto-updated.

Last update: 2024-05-15 03:26:50 UTC


README

Get the geographical localtion of visitor based off Cloudflare headers.

Installation

composer require psicologiaymente/cloudflare-geoip

Configuration

Run the following command to publish configuration. Be sure to modify the configuration file specifically the default location to use if the cloudflare headers are absent.

php artisan vendor:publish --tag=cloudflare-geoip-config

Usage

<?php

namespace App\Http\Controllers;

use PsicologiaYMente\CloudflareGeoIp\Facades\GeoIp;
use App\Models\Users;

class RegisterController extends Controller {
    public function __invoke(): User
    {
        $location = GeoIp::getLocation();

        return User::create([
            'register_ip' => $location->ip
        ]);
    }
}

Location Object

\PsicologiaYMente\CloudflareGeoIp\Location {
    +ip: "192.182.88.29"
    +city: "New Haven"
    +country: "US"
    +continent: "NA"
    +latitude: 41.31
    +longitude: -72.92
    +postalCode: "06510"
    +region: "Connecticut"
    +regionCode: "CT"
    +timezone: "America/New_York"
    // pseudo fields
    +iso_code: 'US' // same as country
    +isoCode: 'US' // same as country
    +state: 'CT' // same as regionCode
    +state_name: 'Connecticut' // same as region
    +stateName: 'Connecticut' // same as region
    +postal_code: '06510' // same as postal_code
    +lat: '41.31' // same as latitude
    +lon: -72.92 // same as longitude

    +toArray(): array
}