问题
I generated authentication controllers and routes using the php artisan make:auth command.
I would like to update a field named last_login in my database whenever a user logs in.
回答1:
I've altered the default Auth to provide the ability to convert users' passwords from an old algorithm to bcrypt (i'm refactoring a legacy app).
The way I did it:
in app\Providers\EventServiceProvider.php:
'Illuminate\Auth\Events\Login' => [
'App\Listeners\LogAuth',
],
i then added the app\Listeners\LogAuth.php file with the following contents
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Attempting;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Auth, App\User, Hash;
class LogAuth {
public function __construct()
{
//
}
public function handle($credentials, $remember, $login)
{
// get the user, update the column, save
}
}
I hope this helps.
来源:https://stackoverflow.com/questions/34880747/alter-laravels-default-log-in-method