I’m new to laravel, I’m creating a login system where a user can login with username or email. When I submit the form, it returns with an error 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>
Please any help would be appreciated. thanks
Advertisement
Answer
Add name attribute to both input fields username and password
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>