How to display error with Session:get() and withErrors()?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Display Validation Errors Correctly Using withErrors() and Session Data in Laravel

As senior developers working with the Laravel ecosystem, handling form validation errors is a fundamental task. When a user submits a form that fails validation, we need a clear, user-friendly way to communicate exactly what went wrong. A common point of confusion arises when trying to bridge the gap between the controller logic (where you set the errors) and the view layer (where you display them).

This post will walk you through the proper, idiomatic Laravel way to use withErrors() and correctly display those messages in your Blade templates, solving the issue where attempts to retrieve session data or error counts return zero.

Understanding the Mechanics of withErrors()

When you utilize validation within a controller method—especially when using Form Requests—Laravel automatically handles storing these errors in the session for flash data purposes. The key is understanding what exactly gets passed and how it's structured.

In your provided example, you are correctly triggering the error display via:

if ($validator->fails()) {
    return Redirect::back()
        ->withErrors($validator) // Passing the entire validator object or its messages
        ->withInput();
}

The crucial realization here is that withErrors() populates the session with an error collection. If you pass the $validator object, Laravel efficiently serializes the validation failures into a format that can be retrieved later.

The Pitfall: Session vs. Direct Data Retrieval

You tried accessing errors using Session::get('MessageBag'). While session management is powerful, relying on raw session keys for complex data structures like validation errors can become cumbersome and error-prone. Furthermore, if you are passing the $validator object directly, retrieving it via session might require specific handling that often misses the mark compared to direct method calls.

The most straightforward approach in Laravel is to leverage the data passed directly from the controller into the view.

The Correct Implementation: Passing Errors Directly

Instead of relying solely on the session for validation messages, the best practice is to pass the error collection directly from your controller to the view. This keeps the data flow explicit and avoids unnecessary session manipulation for simple display tasks.

Step 1: Controller Adjustment

Modify your controller logic to explicitly retrieve the errors you need before redirecting. We will use the messages() method of the validator, which returns an Illuminate\Support\MessageBag.

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

// ... inside your controller method
$validator = Validator::make($request->all(), [
    'name' => 'required|string',
    'email' => 'required|email',
]);

if ($validator->fails()) {
    // Retrieve the error messages immediately
    $errors = $validator->errors(); 
    
    return Redirect::back()
        ->withErrors($errors) // Pass the errors directly to the session
        ->withInput();
}

Step 2: Displaying Errors in the Blade View

In your Blade file, you now need to access the error object passed via the session. Laravel makes this incredibly easy by using the errors() helper function or accessing the session directly if you used withErrors().

To display all errors for a specific field (e.g., 'name'), use the error() helper:

{{-- Displaying errors for a single field --}}
<div>
    <label for="name">Name:</label>
    <input type="text" name="name" value="{{ old('name') }}">
    
    {{-- Check if there are any errors for this specific field --}}
    @error('name')
        <span style="color: red;">{{ $message }}</span>
    @enderror
</div>

{{-- Displaying all errors on a form (useful for debugging or summary) --}}
<h2>Validation Errors Summary</h2>
<ul>
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
</ul>

By using the @error directive, you are directly checking if an error exists for a specific field name (e.g., name). If it does, Laravel automatically exposes the associated message via $message. This is far more robust than trying to count elements in a session variable, ensuring that your application remains clean and adheres to Laravel's conventions.

Conclusion

Displaying validation errors effectively hinges on understanding how Laravel manages flash data. Forget trying to manually parse raw session keys for structure like error messages; instead, embrace the built-in helpers like withErrors() and the @error directive in your Blade files. This approach ensures that your code is readable, maintainable, and leverages the powerful conventions provided by the framework, making your application more robust—a core philosophy behind building scalable applications on platforms like Laravel.