{"id":858,"date":"2022-08-21T10:45:44","date_gmt":"2022-08-21T10:45:44","guid":{"rendered":"https:\/\/appfinz.com\/blogs\/?p=858"},"modified":"2022-08-21T12:05:42","modified_gmt":"2022-08-21T12:05:42","slug":"how-to-get-current-user-location-in-laravel-laravel-tutorial-appfinz-technologies","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/how-to-get-current-user-location-in-laravel-laravel-tutorial-appfinz-technologies\/","title":{"rendered":"How To Get Current User Location In Laravel | Laravel Tutorial | Appfinz Technologies"},"content":{"rendered":"\n<p>In this tutorial, we will show you how to get the current user location in laravel using <a href=\"https:\/\/github.com\/stevebauman\/location\">stevebauman\/location<\/a>, library. Sometimes we need to get the user location according to their IP address so first we will see how do we get it <\/p>\n\n\n\n<p>so lets start coding now, first we will install the latest laravel into our system. at the time i am writting this blog Laravel Version is 9.3.4 <\/p>\n\n\n\n<p>So, here I am using&nbsp;the <strong>stevebauman\/location<\/strong>&nbsp;laravel library package, Using&nbsp;this package you can get information of your user on your website it includes of their IP address, Country Name, Country Code, Postal code,&nbsp;Region,&nbsp;State Name, Country name, Longitude, Latitude etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Install Laravel on your system <\/strong><\/h3>\n\n\n\n<p>In this step create a fresh laravel project so, copy the below command and create a new laravel project if doesn&#8217;t exist.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Terminal&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer create-project --prefer-dist laravel\/laravel location<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"471\" src=\"https:\/\/appfinz.com\/blogs\/wp-content\/uploads\/2022\/08\/user-location-in-laravel-Appfinz-1024x471.png\" alt=\"\" class=\"wp-image-859\" title=\"\" srcset=\"https:\/\/www.appfinz.com\/blogs\/wp-content\/uploads\/2022\/08\/user-location-in-laravel-Appfinz-1024x471.png 1024w, https:\/\/www.appfinz.com\/blogs\/wp-content\/uploads\/2022\/08\/user-location-in-laravel-Appfinz-300x138.png 300w, https:\/\/www.appfinz.com\/blogs\/wp-content\/uploads\/2022\/08\/user-location-in-laravel-Appfinz-768x354.png 768w, https:\/\/www.appfinz.com\/blogs\/wp-content\/uploads\/2022\/08\/user-location-in-laravel-Appfinz.png 1273w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: Install stevebauman\/location Package<\/strong><\/h4>\n\n\n\n<p>After that project setup, we will install&nbsp;stevebauman\/location Package&nbsp;using the below command.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Terminal&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">composer require stevebauman\/location<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: Add Service Provider And Ali<\/strong>as<\/h4>\n\n\n\n<p>After package installation, we need to add&nbsp;service provider and alias in&nbsp;<strong>config\/app.php<\/strong><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"wp-block-preformatted\"><strong>Important<\/strong>: If you're using Laravel 5.5 or above, you can skip the registration of the service provider, as it is registered automatically.<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>Stevebauman\\Location\\LocationServiceProvider::class<\/li><\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Laravel&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">'providers' =&gt; [\n\t\n\tStevebauman\\Location\\LocationServiceProvider::class,\n],\n\n'aliases' =&gt; [\n\t\n\t'Location' =&gt; 'Stevebauman\\Location\\Facades\\Location',\n],<\/pre><\/div>\n\n\n\n<p>You can also do this by this way it will publish the configuration file (this will create a&nbsp;<code>location.php<\/code>&nbsp;file inside the&nbsp;<code>config\/<\/code>&nbsp;directory):<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Terminal&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan vendor:publish --provider=&quot;Stevebauman\\Location\\LocationServiceProvider&quot;<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Add a route<\/h4>\n\n\n\n<p>Now, set the route file this way Add Routes<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;web.php&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">Route::get('\/', [App\\Http\\Controllers\\homeController::class, 'user'])-&gt;name('user');<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: Create Controller<\/strong><\/h4>\n\n\n\n<p>we will display the view and fetch user location using this library in our controller<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;Terminal&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">php artisan make:controller homeController<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;homeController.php&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;?php\n\nnamespace App\\Http\\Controllers;\nuse Illuminate\\Http\\Request;\nuse Stevebauman\\Location\\Facades\\Location;\nclass UserController extends Controller\n{\n    public function userDetails()\n    {\n        $ip = '103.226.226.86'; \/\/For static IP address get\n        \/\/$ip = request()-&gt;ip(); \/\/Dynamic IP address get\n        $data = \\Location::get($ip);                \n        return view('welcome',compact('data'));\n    }\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Step 6: Create a Blade file to show this<\/h4>\n\n\n\n<p>Now, by default there is blade file if not there then create welcome.blade.php file for&nbsp;get current user location details&nbsp;in this path&nbsp;<strong>resources\\views\\welcome.blade.php&nbsp;<\/strong>and&nbsp;add below html code. <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;welcome.blade.php&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;How To Get Current User Location In Laravel - Appfinz Technologies&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body style=&quot;text-align: center;&quot;&gt;\n\t&lt;h1&gt; How To Get Current User Location In Laravel - Appfinz Technologies&lt;\/h1&gt;\n\t&lt;div style=&quot;border:1px solid black; margin-left: 300px; margin-right: 300px;&quot;&gt;\n\t&lt;h3&gt;IP: {{ $data-&gt;ip }}&lt;\/h3&gt;\n\t&lt;h3&gt;Country Name: {{ $data-&gt;countryName }}&lt;\/h3&gt;\n\t&lt;h3&gt;Country Code: {{ $data-&gt;countryCode }}&lt;\/h3&gt;\n\t&lt;h3&gt;Region Code: {{ $data-&gt;regionCode }}&lt;\/h3&gt;\n\t&lt;h3&gt;Region Name: {{ $data-&gt;regionName }}&lt;\/h3&gt;\n\t&lt;h3&gt;City Name: {{ $data-&gt;cityName }}&lt;\/h3&gt;\n\t&lt;h3&gt;Zipcode: {{ $data-&gt;zipCode }}&lt;\/h3&gt;\n\t&lt;h3&gt;Latitude: {{ $data-&gt;latitude }}&lt;\/h3&gt;\n\t&lt;h3&gt;Longitude: {{ $data-&gt;longitude }}&lt;\/h3&gt;\n\t&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre><\/div>\n\n\n\n<p>So, finally, we have got our result here, I hope you liked it !! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to get current user location in laravel via user ip <\/p>\n","protected":false},"author":1,"featured_media":860,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,1],"tags":[117,118,119],"class_list":["post-858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-php","tag-laravel9","tag-laravel9userlocation","tag-locationfetchinlarave"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":6,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"predecessor-version":[{"id":868,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/858\/revisions\/868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media\/860"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}