用于GEOIP的Laravel Middleware抛出错误 - >类Torann \ GeoIP \ Location的对象无法转换为字符串

Working on a Laravel Middleware to include GEOIP detection using Torann\GeoIP and saving the value to a cookie for later use i found the following error:

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

where line 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);
    }
}

Im new to middleware in Laravel:

  1. Is this the correct way of using it?
  2. Additionally, I tested geoip()->getLocation() in view and returns the value, any idea why in middleware Geolocation.php throw the error?

$Many things in this code... Now is this what you are trying to achieve...?

  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);
    }