LoginSignup
0
0

More than 3 years have passed since last update.

Laravel Authのユーザ登録とログインのそれぞれのカスタマイズのやり方

Posted at

ユーザ登録はname, age, tel, address, passwordに変更し、
ログイン画面はtel, passwordに変更する。

ユーザ登録のカスタマイズ

マイグレーションの作成

デフォルト状態から、カラムを変更する。

create_users_table.php
$table->bigIncrements('id');
            $table->string('name');
            $table->integer('age');
            $table->unsignedBigInteger('tel');
            $table->string('address');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

モデルの編集

$fillableのカラムを変更する

User.php
protected $fillable = [
        'name', 'age', 'tel', 'address', 'password',
    ];

コントローラーの編集

バリデーションアクション、新規保存アクションそれぞれを変更する

register.php
protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            // 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'age' => ['required', 'integer', 'max:255'],
            'tel' => ['required', 'numeric', 'digits_between:10,11'],
            'address' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }


protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            // 'email' => $data['email'],
            'age' => $data['age'],
            'tel' => $data['tel'],
            'address' => $data['address'],
            'password' => Hash::make($data['password']),
        ]);
    }

register.blade.phpの変更

メールアドレスの項目を削除し、name項目をコピーして、nameと記載している箇所をすべて、適当なカラム名にする。
例として、age項目のみ下に記載している。

register.blade.php
                        //age 
                        <div class="form-group row">
                            <label for="age" class="col-md-4 col-form-label text-md-right">{{ __('Age') }}</label>

                            <div class="col-md-6">
                                <input id="age" type="number" min="1" class="form-control @error('age') is-invalid @enderror" name="age" value="{{ old('age') }}" required autocomplete="age" autofocus>

                                @error('age')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

以上の編集で新規登録のカスタマイズはできているはず。

ログイン機能のカスタマイズ

bladeファイルの編集

emailの項目を参考に、emailと記載の箇所を全て、telに変更している。

login.blade.php
                        //tel 
                        <div class="form-group row">
                            <label for="tel" class="col-md-4 col-form-label text-md-right">{{ __('Tel') }}</label>

                            <div class="col-md-6">
                                <input id="tel" type="text" class="form-control @error('tel') is-invalid @enderror" name="tel" value="{{ old('tel') }}" required autocomplete="tel" autofocus>

                                @error('tel')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

コントローラの編集

login.blade.phpを開いてみると、ログイン処理のアクション見当たらない。ログイン処理自体は、そのファイル内に記載している、
use AuthenticatesUsers;
の中身で実行されている。だから、そのクラスの中身を変更する必要がある。
以下のアクションを以下のように変更する。
一つ目がログインの際のバリデーション設定で、二つ目はよくわからないが、
emailを対象にしているよっていうことを宣言しているだけだと思う。だからここをtelに変更して、
telがログイン対象だよってことを変更した。

AuthenticatesUsers.php
protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required',
            'password' => 'required|string',
        ]);
    }

public function username()
    {
        return 'tel';
    }

以上の編集でログインカスタムができていると思う。

0
0
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
0