When building forms in a Laravel application, one common challenge developers face is preserving user input after form validation fails. A user fills out a form, submits it, and if validation fails, the page reloads, and all the entered data disappears. The user then has to type everything again, which creates a poor user experience.
This problem can be resolved with a simple helper function called old().
In this blog, we will understand how the old() helper works with a simple practical example.
Prerequisite:
1. Composer (latest Version)
2. Laravel version 12
Steps :
- Create a new Laravel project
- Create routes
- Create a controller
- Use the old() Helper
- Test the form
Step 1: Create a new Laravel Project
First, create a new Laravel project using Composer.
composer create-project laravel/laravel old-helper-demoStep 2: Create Routes
Open the web.php and define two routes: one for displaying the form and another for submitting it.
use App\Http\Controllers\UserController;
Route::get('/register', [UserController::class, 'index']);
Route::post('/register', [UserController::class, 'store'])->name('register.store');Step 3: Create a Controller
php artisan make:controller UserControllerNow, open the created controller and add the following methods.
public function index()
{
return view('register');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email'
]);
return "Form submitted successfully";
}Step 4: Use the old() Helper
<form action="{{ route('register.store') }}" method="POST">
@csrf
<label>Name</label>
<input type="text" name="name" value="{{ old('name') }}">
<label>Email</label>
<input type="email" name="email" value="{{ old('email') }}">
<button type="submit">Submit</button>
</form>Step 5: Test the Form
Now open the browser and visit:
http://127.0.0.1:8000/register
Submit the form without entering data. The page will be reloaded upon failing validation so that any data entered before the validation failure will still be on the page.
Conclusion:
Developers creating forms in the Laravel framework must know how to use the old() helper to improve the user’s experience and create effective form processing. The old() helper makes it easy for users not to lose their input data after a validation failure. It’s a small feature that makes a big difference in building professional and user-friendly applications.
FAQ
1. What does the old() helper do in Laravel?
The old() function allows you to repopulate form fields from previously submitted data that is in the sessions.
2. When does Laravel store old input data?
Laravel stores previously submitted form input automatically in the session when a validation error occurs and when “redirect()->back()->withInput()” is called for the user to redirect back to the form.
3. Can I use old() helper function with all input types?
Yes. Any input types supported by Laravel can leverage this helper function. Example input types include: text, textarea, select options (dropdowns), radio buttons, and checkbox types.



