0

I have the following code in my view

@if ($errors->any())
    <ul class="alert alert-danger">
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@endif

I expect $errors to be available to my view, so I want to display them

However, this block throws the following exception :

ErrorException in 958ab466b0f563093a9e18c3ff070466cc69459a.php line 38: Undefined variable: errors (View: .....filename.blade.php

1
  • Are you sure that the variable $errors is available in the view every time other than in erroneous occasions? I think you should check whether the errors variable is set Commented Jan 5, 2016 at 8:39

1 Answer 1

3

For Laravel 5.2, make sure to put your routes within the web middleware group like this:

Route::group(['middleware' => ['web']], function () {
    // Add your routes here
});

I wrote a more in-depth explanation on why this is the case here: Laravel 5.2 validation errors

In a nutshell, Laravel 5.0 and 5.1 used to run several middlewares automatically. One of these middlewares (\Illuminate\View\Middleware\ShareErrorsFromSession) used to inject the $errors variable automatically into all your views. Laravel 5.2 makes this all optional now, but you can achieve the same effect by simply putting your routes within the web middleware group.

Sign up to request clarification or add additional context in comments.

2 Comments

The Laravel Docs for Validation, have been updated to make this more clear.
Thanks Thomas and lagbox. This fixed it for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.