使用用户名或电子邮件登录 Laravel fortify 验证失败

Login with username or email Laravel fortify fails validation

我是 laravel 的新手,我正在创建一个登录系统,用户可以在其中使用用户名或电子邮件登录。当我提交表单时,它 returns 出现错误 The username field is required.

LoginRequest.php

public function rules()
    {
        return [
            Fortify::username() => 'required|string',
            'password' => 'required|string',
        ];
    }

FortifyServiceprovider.php

Fortify::authenticateUsing(function (LoginRequest $request) {
            $user = User::where('email', $request->username)
                ->orWhere('username', $request->username)->first();

    if ($user && Hash::check($request->password, $user->password)) {
      return $user;
    });

Fortify.php

'features' => [
        Features::registration(),
        Features::resetPasswords(),
        Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication([
            'confirmPassword' => true,
        ]),
    ],

login.blade.php

<form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>

如有任何帮助,我们将不胜感激。谢谢

将名称属性添加到输入字段用户名和密码

login.blade.php

    <form class="mt-4" action="login" method="POST">
@csrf
<div class="mb-3">
   <label class="form-label" for="username">Username</label>
   <input type="text" name="username" value="{{ old('username') }}" class="form-control @error('username') is-invalid @enderror" id="username" placeholder="Enter Username">
   @error('username')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
<div class="mb-3">
   <label class="form-label" for="userpassword">Password</label>
   <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" id="userpassword"
      placeholder="Enter password">
   @error('password')
   <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
   </span>
   @enderror
</div>
</form>