0
2

More than 3 years have passed since last update.

Laravel ログイン時に処理を加える。

Posted at

Laravel 5.5
PHP 7

ログインと同時に処理を行いたいと思い、
調べたのでここに記したいと思います。

vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.phpに
ログイン処理のリダイレクト直前で実行される authenticated() メソッドが書かれていますが、
何も設定されていません。

vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */

protected function authenticated(Request $request, $user)
{
    //
}

これをオーバーライドする形で処理を実装します。
LoginContorollerに処理を実装します。
例、
user情報のtypeが0の人がログインした場合
typeを20に設定するという処理を追加。

Auth/LoginController.php
use App\User;

protected function authenticated(\Illuminate\Http\Request $request, $user)
{
    if ($user->authority == 0) {
        $user->authority = 20;
        $user->save();
}

$requestには、ログイン画面で入力した、メールアドレス(又は、ユーザーネーム)とパスワードが代入されています。
$userには、Userテーブルのユーザー情報が代入されています(ログインユーザーの情報)。
※この変数名は固定です。仮にcustomerテーブルにユーザー情報を保存していたとしてもデータは、$userに代入されます。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2