ユーザ登録はname
, age
, tel
, address
, password
に変更し、
ログイン画面はtel
, password
に変更する。
ユーザ登録のカスタマイズ
マイグレーションの作成
デフォルト状態から、カラムを変更する。
$table->bigIncrements('id');
$table->string('name');
$table->integer('age');
$table->unsignedBigInteger('tel');
$table->string('address');
$table->string('password');
$table->rememberToken();
$table->timestamps();
モデルの編集
$fillableのカラムを変更する
protected $fillable = [
'name', 'age', 'tel', 'address', 'password',
];
コントローラーの編集
バリデーションアクション、新規保存アクションそれぞれを変更する
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項目のみ下に記載している。
//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
に変更している。
//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がログイン対象だよってことを変更した。
protected function validateLogin(Request $request)
{
$request->validate([
$this->username() => 'required',
'password' => 'required|string',
]);
}
public function username()
{
return 'tel';
}
以上の編集でログインカスタムができていると思う。