Last active
August 2, 2018 12:08
-
-
Save Neoglyph/804ed394c3a962e3b9fc4665136e6bf4 to your computer and use it in GitHub Desktop.
Laravel last login event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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