how can i display errors message on blade template of laravel 5
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Displaying Validation Errors in Laravel Blade Templates: A Comprehensive Guide
As a senior developer working with the Laravel ecosystem, I frequently encounter situations where form validation succeeds on the backend, but displaying those errors gracefully on the frontend proves tricky. When you are working with older versions like Laravel 5.2, understanding how Laravel manages session data and error bags is crucial.
The issue you are facing—displaying validation errors in your Blade template—is a very common hurdle. The good news is that Laravel provides robust mechanisms for handling this. By correctly utilizing the error bag passed back from the controller, you can display immediate, user-friendly feedback.
Let’s break down exactly how to fix this issue using your provided examples and ensure you are following best practices.
The Foundation: Controller Validation and Error Bag
The key to displaying errors lies entirely in how you pass the error information from your Controller to the View. Your controller logic for validation is fundamentally correct, but we need to examine how those errors are returned.
In your provided handlelogin method, you correctly use the Validator facade:
// Inside AuthController@handlelogin
if ($validator->fails())
{
return redirect()->back()->withErrors($validator->errors());
}
// ... rest of the logic
The method withErrors($validator->errors()) is the magic. It packages all validation failures into an error bag, which Laravel stores in the session. When you redirect back to the view, this error bag is available on the view side via the $errors variable.
Displaying Errors Gracefully in Blade
The challenge now shifts to iterating through this error bag in your Blade file (user/login.blade.php) to display the messages next to the relevant input fields. Simply checking if any error exists is not enough; you need to check for errors specific to that field.
The Correct Way to Display Errors
Instead of just checking has('email'), which only checks existence, we should iterate over all errors related to a specific field. This ensures you catch every message Laravel has stored.
Here is how you can correctly display validation messages in your HTML form:
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" placeholder="Email">
{{-- Display errors specifically for the 'email' field --}}
@error('email')
<div class="text-danger mt-1">
{{ $message }}
</div>
@enderror
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control">
{{-- Display errors specifically for the 'password' field --}}
@error('password')
<div class="text-danger mt-1">
{{ $message }}
</div>
@enderror
</div>
Explanation of the Blade Syntax:
@error('field_name'): This directive checks if there is an error associated with the specified field name (e.g.,email). If an error exists, the code block inside will execute.{{ $message }}: Inside the@errorblock, Laravel automatically provides the specific validation message stored for that field (e.g., "The email field is required"). This is much cleaner than trying to manually retrieve the error text.
This approach is far superior because it ensures that only relevant errors are displayed, improving user experience significantly. For more complex scenarios involving many fields, you can use a simple loop over $errors->all() if necessary, but targeting individual fields with @error is the most idiomatic and readable solution for form validation feedback.
Conclusion: Adopting Laravel Best Practices
Displaying errors correctly is about understanding the data flow between your Controller and View. By relying on Laravel’s built-in error handling via withErrors(), and using Blade directives like @error to iterate over the error bag, you ensure a robust and maintainable application structure.
As you grow your project, consider moving validation logic out of the controller and into dedicated Form Request classes. This approach aligns perfectly with the principles promoted by the Laravel Company, making your code cleaner, more reusable, and easier to test. Mastering these patterns is what separates functional code from truly professional software development.