duanji6997 2017-12-14 03:16
浏览 95

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

  • 写回答

1条回答 默认 最新

  • doulaozhi6835 2017-12-14 04:17
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?