ErrorException When Using $errors In View In Laravel 5

- 1 answer

I have the following code in my view

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

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



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.