Laravel Larvel错误:Torann\GeoIP\Location类的对象无法转换为字符串
使用LaravelLaravel Larvel错误:Torann\GeoIP\Location类的对象无法转换为字符串,laravel,Laravel,使用LaravelGeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')向数据库发送位置数据时出错。 错误: Torann\GeoIP\Location类的对象无法转换为字符串 这是我的授权登录控制器。如何将GeoIP定位数据插入数据库。请帮帮我 如果我删除此代码'current_location'=>GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'),我不再收
GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')向数据库发送位置数据时出错。
错误:
Torann\GeoIP\Location类的对象无法转换为字符串
这是我的授权登录控制器。如何将GeoIP定位数据插入数据库。请帮帮我
如果我删除此代码'current_location'=>GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'),
我不再收到此错误,每个插入数据库的数据,但我添加此代码,我收到此错误
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use Carbon\Carbon;
use App\User;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use GeoIP;
use Location;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
function authenticated(Request $request, $user)
{
// Chrome, IE, Safari, Firefox, ...
$agent = new Agent();
$browser = $agent->browser();
// Ubuntu, Windows, OS X, ...
$platform = $agent->platform();
$user->update([
'last_signin' => Carbon::now()->toDateTimeString(),
'ip_address' => $request->getClientIp(),
'browser_login' => $agent->browser(),
'browser_version' => $agent->version($browser),
'device_login' => $agent->platform(),
'device_version' => $agent->version($platform),
'current_location' => GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'),
'language' => $agent->languages(),
'root' => $agent->robot(),
'https' => $request->server('HTTP_USER_AGENT'),
]);
}
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest',['except'=>['logout', 'userLogout', 'profile']]);
}
public function userLogout()
{
Auth::guard('web')->logout();
return redirect('/');
}
}
验证路径:
//User Auth Route Function
Auth::routes();
这是因为
GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')
返回Torann\GeoIP\Location
的实例,并试图将其保存为字符串
检查此对象的文档它具有以下形状:
\Torann\GeoIP\Location {
#attributes:array [
'ip' => '232.223.11.11',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.28,
'lon' => -72.88,
'timezone' => 'America/New_York',
'continent' => 'NA',
'currency' => 'USD',
'default' => false,
]
}
您必须选择一种方法将此位置表示为字符串,一种可能的方法是分别保存纬度和经度
如果只需要在DB中使用一列,可以检查一些GeoHashing实现 之所以发生这种情况,是因为
GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')
返回Torann\GeoIP\Location
的实例,并试图将其保存为字符串
检查此对象的文档它具有以下形状:
\Torann\GeoIP\Location {
#attributes:array [
'ip' => '232.223.11.11',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.28,
'lon' => -72.88,
'timezone' => 'America/New_York',
'continent' => 'NA',
'currency' => 'USD',
'default' => false,
]
}
您必须选择一种方法将此位置表示为字符串,一种可能的方法是分别保存纬度和经度
如果只需要在DB中使用一列,可以检查一些GeoHashing实现 您可能试图从错误的实例使用getLocation方法 1.)按以下方式尝试: “使用Torann\GeoIP\GeoIPFacade作为GeoIP” $location=GeoIP::getLocation() 2.)或按Geoip包文档建议的方法尝试()
从这个实例
\Torann\GeoIP\GeoIP
,然后使用GeoIP()->getLocation('27.974.399.65')代码>您可能试图从错误的实例使用getLocation方法
1.)按以下方式尝试:
“使用Torann\GeoIP\GeoIPFacade作为GeoIP”
$location=GeoIP::getLocation()
2.)或按Geoip包文档建议的方法尝试()
从这个实例\Torann\GeoIP\GeoIP
,然后使用GeoIP()->getLocation('27.974.399.65')
这似乎是当前位置
字段中的一个问题,以及如何在数据库中键入它。从我读到的内容来看,我猜您的字段定义为一个字符串,当尝试将记录保存到数据库时,它失败了,因为您尝试保存的数据是一个位置
对象
我建议更改数据库中的当前位置列,使其成为json
类型。
然后,您可以将数据插入为:
$user->update([
'last_signin'=>Carbon::now()->toDateTimeString(),
“ip_地址”=>$request->getClientIp(),
'browser\u login'=>$agent->browser(),
“浏览器版本”=>$agent->version($browser),
'device_login'=>$agent->platform(),
“设备版本”=>$agent->version($platform),
“当前位置”=>json编码(GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')->toArray(),
'language'=>$agent->languages(),
“root”=>$agent->robot(),
'https'=>$request->server('HTTP\u USER\u AGENT'),
]);
这似乎是当前位置
字段中的一个问题,以及如何在数据库中键入它。从我读到的内容来看,我猜您的字段定义为一个字符串,当尝试将记录保存到数据库时,它失败了,因为您尝试保存的数据是一个位置
对象
我建议更改数据库中的当前位置列,使其成为json
类型。
然后,您可以将数据插入为:
$user->update([
'last_signin'=>Carbon::now()->toDateTimeString(),
“ip_地址”=>$request->getClientIp(),
'browser\u login'=>$agent->browser(),
“浏览器版本”=>$agent->version($browser),
'device_login'=>$agent->platform(),
“设备版本”=>$agent->version($platform),
“当前位置”=>json编码(GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')->toArray(),
'language'=>$agent->languages(),
“root”=>$agent->robot(),
'https'=>$request->server('HTTP\u USER\u AGENT'),
]);
如果更改为\\是否有效如果更改为\\是否有效请向我推荐任何将完整数组字符串存储到数据库中的代码。这取决于保存后需要对位置执行的操作,你想用它做什么?我想在Auth user->Session页面上检索整个网站上的数据,就像GitHub会话设置一样。然后你有两个选项:使用geohash保存它,然后在需要时再次检索它。或者创建一个新的位置表,并使用关系保存它:User hasOne location。@Rezrazi建议的选项也是一个很好的建议。请向我推荐任何将完整数组字符串存储到数据库中的代码。这取决于您以后需要对位置做什么,一旦保存,你想用它做什么?我想在Auth user->Session页面上检索整个网站上的数据,就像GitHub会话设置一样。然后你有两个选项:使用geohash保存它,然后在需要时再次检索它。或者创建一个新的位置表,并使用关系保存它:User hasOne location。@Rezrazi建议的选项也是一个很好的方法。这个方法解决了错误,但它不将整个位置数据存储到一列中,它只存储空括号{}
我已经更新了上面的代码,我应该添加一个->t