Laravel geo与Torann \ GeoIP

I try to use GeoIP on Laravel 5.5 and I have problem to get it work.

What I want

  1. I want get users ISO_CODE on visiting any page in my website. So I can use it in my chart as visitors country location.

Issues

  1. I can't get iso_code on my method (provided below)
  2. How can I make it to get data in all my website and not provided static urls?

Codes

My method using Charts

//Header
use GeoIP as GeoIP;

class ChartController extends Controller
{
//.....

//Method
public function index()
    {
      //rest of my codes
      $data = geoip()->getLocation();
      $chart3 = Charts::create('geo', 'highcharts')
             ->title('My nice chart')
             ->elementLabel('My nice label')
             ->labels($data->pluck('iso_code'))
             ->dimensions(1000,500)
             ->responsive(true);
        return view('admin.charts.index', compact('chart3'));
      }
.....
}

Here is default code of charts package to use Geo chart

Charts::create('geo', 'highcharts')
    ->title('My nice chart')
    ->elementLabel('My nice label')
    ->labels(['ES', 'FR', 'RU'])
    ->colors(['#C5CAE9', '#283593'])
    ->values([5,10,20])
    ->dimensions(1000,500)
    ->responsive(false);

Error I get:

Call to undefined method Torann\GeoIP\Location::pluck()

Any idea how can I achieve what I need?

Thanks in advance

Question 1: I can't get iso_code on my method (provided below)

The geoip()->getLocation() returns you a Torann\GeoIP\Location object, and it's not Laravel Collection, hence you cannot use pluck.

Change this line:

    ....
    ->labels($data->pluck('iso_code'))
    ....

Into this instead:

    ....
    ->labels($data->iso_code)
    ....

The Location class implemented the magic method __get so you can just access the attributes of a location by just referring to it like reading the properties of the location object.

Check out the source code of class Location.


Question 2: How can I make it to get data in all my website and not provided static urls?

One way to get the information you need, is to create a separate Service class in your project, and then include it in the controllers that you need the iso_code.