Get Last Login Info of user in laravel

Hello, in some of the cases we require to track the user’s last login activity into our site for that we need to save there login details into our database. Login details can contains last login date/time, location, IP address and more.

So, in this bl…


This content originally appeared on DEV Community and was authored by Snehal

Hello, in some of the cases we require to track the user's last login activity into our site for that we need to save there login details into our database. Login details can contains last login date/time, location, IP address and more.

So, in this blog we are going to save user's last login and its IP address into our database.

Steps to follow -
  • Create Migrations
  • Register Event/Listener
  • Save/Display Last login info

First create a migration files:

php artisan make:migration add_last_login_at_column_to_users_table
php artisan make:migration add_last_login_ip_address_column_to_users_table

Write the below code in migration file

  • for last login field
    $table->timestamp('last_login_at')->nullable();

  • for last last_login_ip_address field
    $table->timestamp('last_login_ip_address')->after('last_login_at')->nullable();

I am using Laravel default scaffolding which gives us login and registration blade.

Go to the Laravel documentation and search Authentication in that go to Event you will see the Login Event/Listener

'Illuminate\Auth\Events\Login' => [
        'App\Listeners\LogSuccessfulLogin',
    ],

We are going to create our own Listener, so that when user logged in we will save its login details. Register this Event in EventServiceProvider into $listen event listener mapping.

protected $listen = [
 'Illuminate\Auth\Events\Login' => [
        'App\Listeners\UserLoginAt',
  ],
]

After that run this command: It will create Listener file UserLoginAt.

php artisan event:generate

Open UserLoginAt listener file and in handle method write the below code.

use Carbon\Carbon;

public function handle(Login $event)
{
    $event->user->update([
       'last_login_at => Carbon::now(),
       'last_login_ip_address' => request()->getClientIp()
    ]);
}

This is the simple code we require to store user login details into our database.

Now we can access this information anywhere into our project, by using below code. I am accessing it in dashboard.blade.php file

{{ auth()->user()->last_login_at->diffForHumans() }}

Thank you for reading. ??


This content originally appeared on DEV Community and was authored by Snehal


Print Share Comment Cite Upload Translate Updates
APA

Snehal | Sciencx (2021-09-10T18:55:08+00:00) Get Last Login Info of user in laravel. Retrieved from https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/

MLA
" » Get Last Login Info of user in laravel." Snehal | Sciencx - Friday September 10, 2021, https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/
HARVARD
Snehal | Sciencx Friday September 10, 2021 » Get Last Login Info of user in laravel., viewed ,<https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/>
VANCOUVER
Snehal | Sciencx - » Get Last Login Info of user in laravel. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/
CHICAGO
" » Get Last Login Info of user in laravel." Snehal | Sciencx - Accessed . https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/
IEEE
" » Get Last Login Info of user in laravel." Snehal | Sciencx [Online]. Available: https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/. [Accessed: ]
rf:citation
» Get Last Login Info of user in laravel | Snehal | Sciencx | https://www.scien.cx/2021/09/10/get-last-login-info-of-user-in-laravel/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.