Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel last login event
<?php
// thanks to: https://stevenwestmoreland.com/2017/03/recording-last-login-information-using-laravel-events.html
<?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();
//
}
}
---------------------------------------------------------------------------------
$ php artisan event:generate
---------------------------------------------------------------------------------
<?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();
}
}
---------------------------------------------------------------------------------
$ php artisan make:migration add_last_login_at_to_users_table --table=users
$ php artisan make:migration add_last_login_ip_to_users_table --table=users
---------------------------------------------------------------------------------
<?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']);
});
}
}
---------------------------------------------------------------------------------
$ php artisan migrate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment