Help us understand the problem. What is going on with this article?

【Laravel】メアド以外でログインしたい

More than 1 year has passed since last update.

標準の

$ php artisan make:auth

でログインを作るとメアドとパスワードでログインするようになる。
メアドではなく name でログインする方法
LoginController に以下の関数を追加する。別のフィールドをログインに使う場合はそのフィールド名を記述

app/Http/Controllers/Auth/LoginController.php

+    public function username()
+    {
+        return 'name';
+    }

ログイン画面をカスタマイズする。
resources/views/auth/login.blade.php

-                        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
-                            <label for="email" class="col-md-4 control-label">メールアドレス</label>
-
-                            <div class="col-md-6">
-                                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
-
-                                @if ($errors->has('email'))
-                                    <span class="help-block">
-                                        <strong>{{ $errors->first('email') }}</strong>
-                                    </span>
-                                @endif
-                            </div>
-                        </div>
+                        <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
+                            <label for="name" class="col-md-4 control-label">名前</label>
+
+                            <div class="col-md-6">
+                                <input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>
+
+                                @if ($errors->has('name'))
+                                    <span class="help-block">
+                                        <strong>{{ $errors->first('name') }}</strong>
+                                    </span>
+                                @endif
+                            </div>
+                        </div>

この他にもユーザー名が重複しないようバリデーションチェックを入れるとかの対応は必要だが、とりあえずこれで name でログインが可能になる。

usersテーブルのemailフィールド同様、nameフィールドも unique にしておいた方が良いかもね。

- 目次 -

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした