Recording last login information using Laravel events
Laravel’s events provide a simple observer implementation, allowing you to listen for various events that occur in your application. Laravel raises several events throughout the authentication process. This example shows how to attach a listener for the Illuminate\Auth\Events\Login event and store when a user has last logged in and from what IP address.
Registering events
Edit the EventServiceProvider included in your Laravel application to register the event listener.
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogSuccessfulLogin',
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
Use the artisan event:generate command to generate the LogSuccessfulLogin listener.
$ php artisan event:generate
Events and listeners generated successfully!
Edit the LogSuccessfulLogin listener to add the event handling logic.
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Login;
use Illuminate\Http\Request;
class LogSuccessfulLogin
{
/**
* Create the event listener.
*
* @param Request $request
* @return void
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Handle the event.
*
* @param Login $event
* @return void
*/
public function handle(Login $event)
{
$user = $event->user;
$user->last_login_at = date('Y-m-d H:i:s');
$user->last_login_ip = $this->request->ip();
$user->save();
}
}
Database migrations
The above implementation records the last login information in two columns on the users table. Use the artisan make:migration command to create the migrations for each of the columns that will be added to the users table.
$ php artisan make:migration add_last_login_at_to_users_table --table=users
Created Migration: 2017_03_07_173328_add_last_login_at_to_users_table
$ php artisan make:migration add_last_login_ip_to_users_table --table=users
Created Migration: 2017_03_07_173333_add_last_login_ip_to_users_table
Edit the migrations to add the last_login_at
and the last_login_ip
columns.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLastLoginAtToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->timestamp('last_login_at')->nullable()->after('remember_token');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['last_login_at']);
});
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddLastLoginIpToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('last_login_ip')->nullable()->after('last_login_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['last_login_ip']);
});
}
}
Use the artisan migrate command to run the migrations.
$ php artisan migrate
Migrated: 2017_03_07_173328_add_last_login_at_to_users_table
Migrated: 2017_03_07_173333_add_last_login_ip_to_users_table