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:
- Is this the correct way of using it?
- Additionally, I tested
geoip()->getLocation()
in view and returns the value, any idea why in middleware Geolocation.php throw the error?