12_2_usersテーブルに最終ログイン日時の保存 - hpscript/laravel GitHub Wiki

初期

usersにlast loginカラムを追加

mysql> describe users;

$ php artisan make:migration add_column_last_login_at_users_table --table=users ***_add_column_last_login_at_users_table.php

public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->timestamp('last_login_at')->nullable()->after('remember_token')->comment('最終ログイン');
        });
    }

public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            //
            $table->dropColumn('last_login_at');
        });
    }

$ php artisan migrate mysql> describe users;

Logined eventとLastLoginListeners listenerを作成

app/Providers/EventServiceProvider.php

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        // ログイン時にイベント発行
        'App\Events\Logined' => [
            // 最終ログインを記録するリスナー
            'App\Listeners\LastLoginListener',
        ],
    ];

$ php artisan event:generate

listenerで、lastlogin日時をDBに保存する

  • event側は処理なし app/Listeners/LastLoginListener.php
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
public function handle(Logined $event)
    {
        //
        $user = Auth::user();
        $user->last_login_at = Carbon::now();
        $user->save();
    }

LoginControllerでeventを呼び込む

app/Http/Controllers/Auth/LoginController.php

use Illuminate\Http\Request;
use App\Events\Logined;
protected function authenticated(Request $request, $user)
    {
        event(new Logined());
    }
mysql> select * from users;
+----+---------+--------------+------------------+-------------------+--------------------------------------------------------------+--------------------------------------------------------------+---------------------+---------------------+---------------------+
| id | role_id | name         | email            | email_verified_at | password                                                     | remember_token                                               | last_login_at       | created_at          | updated_at          |
+----+---------+--------------+------------------+-------------------+--------------------------------------------------------------+--------------------------------------------------------------+---------------------+---------------------+---------------------+
|  1 |       1 | 田中太郎     | tanaka@***.com | NULL              | **** | **** | 2020-02-21 09:50:03 | 2020-02-20 08:51:14 | 2020-02-21 09:50:03 |
|  2 |       2 | 山田一郎     | yamada@***.com | NULL              | **** | **** | NULL                | 2020-02-20 08:52:15 | 2020-02-20 08:52:15 |
+----+---------+--------------+------------------+-------------------+--------------------------------------------------------------+--------------------------------------------------------------+---------------------+---------------------+---------------------+
2 rows in set (0.00 sec)

公式ドキュメント:https://readouble.com/laravel/6.x/ja/authentication.html