Laravel Fortify でログイン方法をメールアドレスから別のカラムに変更したい人向け。
簡単2ステップで、終わりです。
①FortifyServiceProviderに追記する
FortifyServiceProviderのbootメソッドに下記の通り、追加
FortifyServiceProvider.php
public function boot()
{
Fortify::authenticateUsing(function (Request $request) {
$user = User::whereLoginId($request->login_id)->first();
if ($user &&
Hash::check($request->password, $staffMember->password)) {
return $user;
}
});
}
②config/fortify.phpを変更する
'username'というのがどこかにありますので、それを'email'から指定したいカラムに変更します。
config/fortify.php
//'username' => 'email', // デフォルト
'username' => 'login_id',
これで完了です。
公式ドキュメントも読んでみてください。
https://laravel.com/docs/9.x/fortify#customizing-user-authentication