Laravel GEOIP 的中间件抛出错误 -> class Torann\GeoIP\Location 的对象无法转换为字符串

Laravel Middleware for GEOIP throw error -> Object of class Torann\GeoIP\Location could not be converted to string

在 Laravel 中间件上工作以包括使用 Torann\GeoIP 的 GEOIP 检测并将值保存到 cookie 供以后使用我发现以下错误:

ErrorException in Geolocation.php line 21: Object of class Torann\GeoIP\Location could not be converted to string

第 21 行 -> if($request->$location == null){...

namespace App\Http\Middleware;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use GeoIP;

class Geolocation
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Test to see if the requesters have an ip address.
        $location = GeoIP::getLocation();
        if($request->$location == null){
           Cookie::queue('price_geo', 'US','3600');
        }
        Cookie::queue('price_geo', $location->getAttribute('iso_code'),'3600');

        return $next($request);
    }
}

我是 Laravel 中的中间件新手:

  1. 这是正确的使用方法吗?
  2. 此外,我在视图中测试了 geoip()->getLocation() 和 returns 值,知道为什么在中间件 Geolocation.php 中抛出错误吗?

$此代码中有许多内容... 这就是您要实现的目标吗...?

  public function handle($request, Closure $next)
    {
        // Test to see if the requesters have an ip address.
        $location = GeoIP::getLocation($request->ip());
        if(isset($location){
           Cookie::queue('price_geo', $location->getAttribute('iso_code'),'3600');

        } else {
           Cookie::queue('price_geo', 'US','3600');
        }


        return $next($request);
    }