Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
18
Help us understand the problem. What is going on with this article?
@nekyo

【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 にしておいた方が良いかもね。

- 目次 -

18
Help us understand the problem. What is going on with this article?
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
nekyo

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
18
Help us understand the problem. What is going on with this article?