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

Thomas Kim
  • 15,326
  • 2
  • 52
  • 42
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
  • 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 – Imesha Sudasingha Jan 05 '16 at 08:39

1 Answers1

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.

Community
  • 1
  • 1
Thomas Kim
  • 15,326
  • 2
  • 52
  • 42
  • The [Laravel Docs for Validation](https://laravel.com/docs/5.2/validation#quick-displaying-the-validation-errors), have been updated to make this more clear. – lagbox Jan 05 '16 at 08:40
  • Thanks Thomas and lagbox. This fixed it for me. – Arunabh Das Jan 05 '16 at 08:51