How to fetch visitors location using ip2location in Laravel

If you have installed the ip2location library for Laravel as described in the previous post then you can proceed with the next step i.e. using it in your controller to fetch the visitor’s location data from their IP address.

  • First I will create a controller by the name of HomeController.
    php artisan make:controller HomeController
  • Open the new controller and add a reference to IP2LocationLaravel class.
  • Next pass in the ip address of the visitor to the get method of IP2LocationLaravel class.
  • The function will return visitor location data as an array which you can use in your code.

You can fetch complete code for the HomeController.php from GitHub

Hope the above helped.

Please follow and like us:
error