ขั้นตอนที่ 1.
ทำการแก้ไขฐานข้อมูลโดยเพิ่มฟิลด์ username ที่ตาราง users ในฐานข้อมูล
ขั้นตอนที่ 2.
ไปที่ไฟล์ app/Models/User.php แล้วเพิ่มคอลัม username ลงในฟังก์ชัน Fillable
ขั้นตอนที่ 3.
ไปที่ไฟล์ resources/views/auth/register.blade.php แล้วเพิ่มโค้ดนี้ลงไปในบรรทัดต่อจากช่องกรอกข้อมูลของ name
โค้ด: เลือกทั้งหมด
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" name="username" value="{{ old('username') }}" required autofocus>
@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>
ขั้นตอนที่ 4.
ไปที่ไฟล์ resources/views/auth/login.blade.php แล้วนำโค้ดไปแทนที่โค้ดช่องสำหรับกรอก Email สำหรับการ Login อันเก่าได้เลย
โค้ด: เลือกทั้งหมด
<div class="form-group row">
<label for="login" class="col-md-4 col-form-label text-md-right">Username Or Email</label>
<div class="col-md-6">
<input id="login" type="text" class="form-control
{{ $errors->has('username') || $errors->has('email') ? ' is-invalid' : '' }}" name="login" value="{{ old('username') ?: old('email') }}" autofocus>
@if ($errors->has('username') || $errors->has('email'))
<span class="invalid-feedback">
<strong>{{ $errors->first('username') ?: $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
ขั้นตอนที่ 5.
ไปที่ไฟล์ app/Http/Controller/Auth/RegisterController.php 5.1. ทำการเขียนคำสั่งเพื่อตรวจสอบว่าข้อมูล username ที่กรอกเข้ามาถูกต้องหรือไม่
5.2. ทำการเขียนคำสั่งเพื่อเพิ่มข้อมูล username ที่กรอกเข้ามาลงในฐานข้อมูล
ขั้นตอนที่ 6.
ไปที่ไฟล์ app/Http/Controller/Auth/LoginController.php แล้วทำการใส่โค้ดตามนี้ได้เลย
โค้ด: เลือกทั้งหมด
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
protected $username;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->username = $this->findusername();
}
public function findusername()
{
$login = request()->input('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$field => $login]);
return $field;
}
public function username()
{
return $this->username;
}
}
เพียงเท่านี้ก็สามารถทำการ Login ได้ทั้ง Email และ Username ได้แล้วครับหากท่านต้องการที่จะปรับแต่งหน้าต่างให้มีความสวยงามกว่านี้ก็ให้นำ Bootstrab หรือ CSS เข้ามาใช้เพื่อช่วยทำให้หน้าต่างสวยงามมากยิ่งขึ้น
อ้างอิง
https://codebriefly.com/laravel-authentication-with-username-or-email/
https://hdtuto.com/article/login-with-username-or-email-in-laravel-6
https://dev.to/pramanadiputra/laravel-how-to-let-user-login-with-email-or-username-j2h