stevebauman / location

Detect a users location by their IP Address.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get device value

shankiflang opened this issue · comments

Hi,

Is it possible to add the device type (mobile = true/false) ?

I see that https://ip-api.com/ provide this.

Thanks

Hi @shankiflang,

Unfortunately no -- you would have to create your own driver that extends the built-in IP-API driver, and create your own Position instance that sets the mobile property.

I'd gladly accept PR's though!

Hello,

Thank you for this info. Would it be possible to have more precision as to how to do this properly?

Thank you

For sure @shankiflang! I've just pushed an update that will make this much easier. It is a new configuration option named position, which contains the name of the class that is returned by drivers.

Give me a moment to make a new release on GitHub with the new patch, run composer update to pull it in, and then proceed with the steps below.

First, create your own Position class that extends the current one, and add your $mobile property to it:

Note: I've created a new Location folder inside of the app folder where these classes will reside.

namespace App\Location;

use Stevebauman\Location\Position as BasePosition;

class Position extends BasePosition
{
    public $mobile = false;
}

Then, create your own IpApi driver that extends the built-in one.

Set the new position property in the hydrate method:

use App\Location\Position;
use Stevebauman\Location\Drivers\IpApi as BaseIpApi;

class IpApi extends BaseIpApi
{
    public hydrate(Position $position, Fluent $location)
    {
        $position = parent::hydrate($position, $location);

        $position->mobile = $location->mobile == 'true' ? true : false;

        return $position;
    }
}

Once complete, update your config/location.php configuration by setting your driver to your own custom IpApi driver class name, and add the position option and populate it with the class name of your custom position:

// config/location.php

return [

    // ...

    'driver' => \App\Location\IpApi::class,

    'position' => \App\Location\Position::class,

];

Please remember that the mobile field is not returned by default, and has to be requested with the fields parameter.

API docs

Thanks @vladc! In such case, we can then modify the url() method inside of the custom IpApi driver:

use App\Location\Position;
use Stevebauman\Location\Drivers\IpApi as BaseIpApi;

class IpApi extends BaseIpApi
{
    //...

    protected function url($ip)
    {
        return "http://ip-api.com/json/$ip?fields=mobile,lat,lon,etc...";
    }
}

Hi @shankiflang,

Release v5.1.0 is now out with this patch. You're all set. 🎉