What is the best practice to show old value when editing a form in Laravel?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Best Practices for Displaying Old Values While Editing Forms in Laravel
Body:
When developing forms in Laravel, it's essential to consider different scenarios and provide users with a smooth experience. One such requirement is displaying the old value of fields when an edit form fails validation or input errors occur. In this article, we will discuss the best practices for accomplishing this using Laravel framework.
Using the Form Request Validation Class
By utilizing the built-in Form Request Validation class in Laravel, you can easily access both validated and unvalidated data. This class extends the base ModelBinding class and provides methods for working with HTTP requests and performing validation. Here's an example of the FormRequest class:class EditDogFormRequest extends FormRequest {
// ... other validation rules, etc.
public function attributes() {
return [
'title' => 'Dog Name',
'age' => 'Age',
// ... more fields if required
];
}
}
Displaying Old Data in Form Fields
To display the old data when editing a form, you can use the "old" function in your Blade template. This will return either the submitted value from the previous request or the default value for that field if it has not been modified:@if($errors->any())
@foreach ($errors as $field => $error)
{{$error}}
@endforeach
@endif
// ... other fields
Handling Null Values
In the above example, if no data was entered for a particular field during validation or there were input errors, the "old" function will return an empty string instead of the old value. This can be handled by setting the model attribute to null when updating it based on the submitted request. For example:$dog->update(request()->validate([
// ... validation rules here
], ['title' => 'The :attribute field is required']));
If no errors occur during validation, this code will update the dog model with the new input data. If an error occurs and validation fails, however, it will not update any values, leaving the old data intact.