Store and display the location of the user in 8 steps using laravel.

Subscribe to my newsletter and never miss my upcoming articles

In this blog we will see how to store the location of the user when user logs in.

1. Create a Project. Type below command in your terminal.

composer create-project --prefer-dist laravel/laravel UsersOnline

You will see something like below image after sucessfull installation of your project.

install 1.PNG

2. Now Install Auth. To Install Auth type below command in your terminal.Install one by one.

composer require laravel/ui

auth 1.PNG

php artisan ui vue --auth
npm install
npm run dev

3. Install the location package. Type below command in your terminal.

composer require stevebauman/location

You can see something like below image.

location package.PNG

You can the package has been installed in composer.json file.


4. Go to app.php file in config/app.php add the service provider by typing the below line in providers array.

    'providers' => [
         * Laravel Framework Service Providers...

Also the below line in aliases in app.php

    'aliases' => [
        'Location'=> Stevebauman\Location\Facades\Location::class,

5. Type the below command in your terminal.

php artisan vendor:publish

If you see something like this and press 0(ZERO) or else continue will step 6.

vendor 1.PNG Successfull publish looks like below image.

vendor 1.PNG

6. Add location column in your user table.* Go to database/migrations/create_user_table.php*


run below command in your terminal for migration.

php artisan migrate

7. Go the HomeController in app/Http/Controllers/HomeController.php. Add below code

use App\Models\User;
use Stevebauman\Location\Facades\Location;
use Illuminate\Support\Facades\Auth;

Make sure add above code on top.

    public function index()
         $user = User::findOrFail(Auth::user()->id);
        $users = User::all();
        $ip = $_SERVER['REMOTE_ADDR'];
        $data = Location::get($ip);
        $location = serialize($data);  // serializing to store in database
        $user_location = unserialize($user->location); // unserialize to display the location
        return view('home',compact('user_location'));

8. Go to home.blade.php in resourse/views/home.blade.php. Add below code.

The Location of login user is:

Register a user and you will see something like this in the browser.

output 3.jpg

On xampp you will see location as false.

No Comments Yet