jcadima
8/21/2018 - 4:02 PM

Laravel Add last login user data


https://laraveldaily.com/save-users-last-login-time-ip-address/
<?php
1) add these 2 columns to 'users' table
php artisan make:migration add_login_fields_to_users_table

2) fill out our new migration table:
    public function up()
    {
        Schema::table('users', function( Blueprint $table) {
            $table->datetime('last_login_at')->nullable() ;
            $table->string('last_login_ip')->nullable();
        }) ;
    }
    
    
3) php artisan migrate

4) Open App\User.php Model and add those 2 to the fillable array
    protected $fillable = [
        'name', 'email', 'password', 'admin', 'last_login_at', 'last_login_ip'
    ];


5) Open app/Http/Controllers/Auth/LoginController.php

at the top:
use Carbon\Carbon;
use Illuminate\Http\Request;

now add this new method:
  
public function authenticated( Request $request, $user ) {
    $user->update([
        'last_login_at' => Carbon::now()->toDateTimeString(),
        'last_login_ip' => $request->getClientIp()
    ]) ;
}

Now logout and login again , you will see last login and IP generated after each
new login