Laravel错误:Torann \ GeoIP \ Location类的对象无法转换为字符串

时间:2019-05-06 09:48:01

标签: laravel

使用Laravel GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')将位置数据发送到数据库时出现错误,

错误:

  

Torann \ GeoIP \ Location类的对象无法转换为字符串

这是我的Auth LoginController。如何将GeoIP Loacation数据插入数据库。请帮助我

如果我删除此代码'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();

3 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为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,
    ]
}

您必须选择一种将该位置表示为字符串的方法,一种可能的方法是分别保存纬度和经度。

如果仅需要在数据库中使用一列,则可以检查一些GeoHashing实现skthon/geogash

答案 1 :(得分:0)

您可能正在尝试从错误的实例使用getLocation方法。

1。)按照以下方式尝试:

“将Torann \ GeoIP \ GeoIPFacade用作GeoIP” $ location = GeoIP :: getLocation();

2。)或尝试按此处建议的Geoip软件包文档(http://lyften.com/projects/laravel-geoip/doc/methods.html) 从此实例\Torann\GeoIP\GeoIP开始,然后使用geoip()->getLocation('27.974.399.65');

答案 2 :(得分:0)

这似乎是current_location字段中的一个问题,以及在数据库中如何键入它。从我阅读的内容来看,我猜您的字段已定义为字符串,并且在尝试将记录保存到数据库时,由于您要保存的数据是Location对象,因此该字段失败。

我建议您将数据库中的current_location列更改为json类型。

然后,您可以将数据插入为:

    $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' => json_encode(GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')->toArray()),
        'language' =>  $agent->languages(),
        'root' => $agent->robot(),
        'https' => $request->server('HTTP_USER_AGENT'),
    ]);