【问题标题】:how to get country from ip in Laravel如何从 Laravel 中的 ip 获取国家/地区
【发布时间】:2022-06-25 15:11:13
【问题描述】:

我创建了自定义分析模块,我可以在其中跟踪访客活动,这里我使用 Laravel 包“stevebauman/location”来获取访客位置。

主要问题是,当我静态地向变量提供 IP 时,这个 ip 为我提供了正确的位置。另一方面,当我动态获取 IP 时。它只是提供IP

$visitor = request()->ip();

如何从 Laravel 中的 IP 获取国家名称、代码、邮政地址

$visitor = request()->ip();
    $traffic = PageVisits::where('ip_address', $visitor)->where('property_id', $id)
    ->whereMonth('created_at', Carbon::now()->month)->first();
    if(auth()->check() && auth()->user()->usertype == 'Admin'){
    
    }else{
        if (!$traffic) {

            $traffic = new PageVisits();
            $traffic->ip_address = $visitor;
            $traffic->property_id = $id;
            $position = Location::get('https://'.$visitor);
            $traffic->country = $position->countryName;
            $traffic->agency_id = $property->agency_id ?? '';
            $traffic->save();
        }

【问题讨论】:

标签: php mysql laravel ip laravel-7


【解决方案1】:

您可以创建一个服务,然后可以在您的控制器中使用该服务。

【讨论】:

  • 请分享完整的解决方案
  • 你能分享答案吗?
  • 好的,我要送你
猜你喜欢
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2017-04-25
  • 2013-10-24
  • 1970-01-01
相关资源
最近更新 更多