How can I redirect with old input in Laravel?

Stefan Bogdanescu
Founder & Senior Architect · 12 July 2026

Title: Efficient Redirection with Preserved Inputs in Laravel for Login Fails

Introduction

When users experience login failure due to incorrect credentials, redirecting them back to the login form while displaying error messages is essential to enhance user experience and improve security. In this post, we’ll discuss how to perform efficient redirections with old input preservation in Laravel, ensuring that your users can easily attempt another login without losing their previous input data.

Redirection with Error Messages

Firstly, you need to set up the failed authentication logic within your controller:

if (Auth::attempt(request(['username', 'password'])) !== true) {
    return Redirect::to('/')
        ->with('error', 'Username/Password Wrong')
        ->withInput(Request::except('password'))
        ->withErrors($validator);
}

Here, we’ve used Auth::attempt() to try logging in the user. If it fails, we redirect back to the login page using Laravel’s Redirect class. This ensures that the user is redirected to the correct route as specified. We also pass with('error', 'Username/Password Wrong'), which will display an error message on the form. The ‘except’ parameter in Request::except('password') instructs Laravel to exclude the ‘password’ field from being sent along with the request, preventing potential security vulnerabilities.

Handling Old Input Data

In your login form (Blade template), you can use the Laravel Form facade to handle input data:

{!! Form::open(array('url' => '/', 'class' => 'login-form')) !!}

  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" class="form-control" id="username" name="username" placeholder="Enter Username" required>
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required>
  </div>
  <button type="submit" class="btn btn-primary">Login</button>

{!! Form::close() !!}

This code shows a basic login form that handles the input data, while preserving the old data when an error occurs. However, the previous input is not displayed on the page by default. To show your users the last entered values, you can simply add the following lines before closing the Form:

{{ $errors->has('username') ? '

' . $errors->first('username') . '

' : '' }} @if (old('email')) @else @endif

Here, we’ve checked for the presence of input error messages using {{ $errors->has('username') ? '<p class="help-block">' . $errors->first('username') . '</p>' : '' }}. Then, we use Laravel’s old() method to retrieve and display the old input values. If there is no previous value, Laravel won’t output anything.

Conclusion

In conclusion, you can easily achieve efficient redirection with preserved input data for login failure scenarios in your Laravel application. By implementing proper error handling, showing relevant messages, and ensuring that users retain their last attempted input values, you provide a better user experience while maintaining the highest level of security. With these steps, you’ll be able to create seamless authentication flows within your web app.

Our network